• Uncategorized

About node.js : nodejs-5500-port-API-get-connect-refused

Question Detail

Using fastify to build some nodejs backend API, here is part of my code snippet:

fastify.get('/', async (request, reply) => {
        return { data: 'ok...' }
})

Deploy it on my Tencent Clound Server

Install some related dependencies, and using pm2 to keep my application online. Using curl to test my API currectly:

curl http://localhost:5500/

{"data":"ok..."}

But when I tried to request my test api using public ip address: http://1.xx.xx.xx:5500/, but it reminded me: connect refused.

And I tried to use curl to test my API by 127.0.0.1, get the same error:

curl http://127.0.0.1:5500/
//  connect refused

P.S:

I have disabled firewall:

And the 5050 port is open on my cloud server:

Question Answer

You should set listen at address 0.0.0.0

fastify.listen(5500, '0.0.0.0', function (err, address) {
  if (err) {
    fastify.log.error(err)
    process.exit(1)
  }
  fastify.log.info(`server listening on ${address}`)
})

Link at Note

The above examples, and subsequent examples in this document, default
to listening only on the localhost 127.0.0.1 interface. To listen on
all available IPv4 interfaces the example should be modified to listen
on 0.0.0.0 like so:

fastify.listen(3000, '0.0.0.0', function (err, address) {
  if (err) {
    fastify.log.error(err)
    process.exit(1)
  }
  fastify.log.info(`server listening on ${address}`)
})

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.