0

I'm trying to setup a feature flag for a Typescript project. The code below is from the https://www.npmjs.com/package/unleash-client. Where I create and connect to an unleash instance. This is all with a locally running unleash setup as per this documentation: https://github.com/Unleash/unleash. It seems from the error, I'm not able to connect to unleash. I have verified the instance is running locally in the docker container as per the docs. I can also see the service up and running in my browser. Would anyone know why I'm getting the error below when I try to connect?

CODE:

import express from 'express';
import { Unleash } from 'unleash-client';

const unleash = new Unleash({
  url: 'http://localhost:4242/api/',
  appName: 'default',
  customHeaders: { Authorization: 'default:development.unleash-insecure-api-token' },
});

ERROR LOG:

FetchError: Unleash Repository error: request to http://localhost:4242/api/client/features failed, reason: connect ECONNREFUSED 127.0.0.1:4242
app-local-backend                  | [1]     at ClientRequest.<anonymous> (/app/node_modules/minipass-fetch/lib/index.js:130:14)
app-local-backend                  | [1]     at ClientRequest.emit (node:events:517:28)
app-local-backend                  | [1]     at Socket.socketErrorListener (node:_http_client:501:9)
app-local-backend                  | [1]     at Socket.emit (node:events:517:28)
app-local-backend                  | [1]     at emitErrorNT (node:internal/streams/destroy:151:8)
app-local-backend                  | [1]     at emitErrorCloseNT (node:internal/streams/destroy:116:3)
app-local-backend                  | [1]     at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
app-local-backend                  | [1]   code: 'ECONNREFUSED',
app-local-backend                  | [1]   errno: 'ECONNREFUSED',
app-local-backend                  | [1]   syscall: 'connect',
app-local-backend                  | [1]   address: '127.0.0.1',
app-local-backend                  | [1]   port: 4242,
app-local-backend                  | [1]   type: 'system'
app-local-backend                  | [1] }

What is weirder is that I can access the endpoint as per the documentation in postman as may be seen below:

Successful GET to unleash API

Any assistance with this would be much appreciated!

2 Answers 2

1

I tried reproducing the issue but was unable to. Your code looks correct to me.

Is it possible you're running your application in isolation, e.g. in a separate Docker container, so it is unable to reach localhost:4242 on your host machine?

If you can reach Unleash at localhost:4242 through your browser and Postman, then I would suggest you start by trying to create a new local project just to see if that works. Something like:

import { initialize } from 'unleash-client'

const TOGGLE = 'unleash-node-test'

const unleash = initialize({
  url: 'http://localhost:4242/api',
  appName: 'unleash-node-test',
  customHeaders: {
    Authorization:
      'default:development.unleash-insecure-api-token'
  }
})

const checkToggles = () => {
  const enabled = unleash.isEnabled(TOGGLE)
  const variant = unleash.getVariant(TOGGLE)
  console.log(TOGGLE)
  console.log('isEnabled', enabled)
  console.log('getVariant', variant)
  setInterval(checkToggles, 5000)
}

unleash.on('ready', checkToggles)

If it works, then I would look into any specificities of the environment you're running your other application in and try to address them.

Sign up to request clarification or add additional context in comments.

Comments

1

This may be due to ipv6 shenanigans. First attempt at connection will be ipv6 which will fail and ipv4 isn't attempted. Try adding this somewhere before you attempt connection.

dns.setDefaultResultOrder('ipv4first');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.