use nodejs API on port 6000

April 3, 2018 774 views
Node.js Ubuntu 16.04

i setup a nodejs droplet. installed pm2 and copied the files to a folder with filezilla. also created a firewall to allow traffic on port 6000 and linked it to the droplet. ran the app with pm2 start app.js

note: this is my first time using digitalocean
note: tried doing calls to my ipv4 address aswell as the floating one

1 Answer

By default node.js will listen only on localhost/127.0.0.1 for local requests. If you specify the IP address in your app as 0.0.0.0 it will listen on all IPs. Another option is to enter only the IP it will be used with (if using a floating IP this is the anchor IP, not the public floating IP users will use).

This shows an example that should listen publicly on port 6000 as long as there is not a firewall blocking it.

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(6000, "0.0.0.0");
console.log('Server running at http://0.0.0.0:6000/');
Have another answer? Share your knowledge.