Search code examples
node.jsexpressrequestjs

Why is this simple express server running on two ports?


I have simple node server, like so:

var express = require('express');  
var request = require('request');
var apiServerHost = "http://<IP address>:3000/api#!/"


var app = express();  
app.use('/', function(req, res) {  
  var url = apiServerHost + req.url;
  req.pipe(request(url)).pipe(res);
});

app.listen(3001); 

I run it with node server.js. Then in browser both port 3001 and 3000 have access to the server. If I replace 3001 with anything else, it will run on 3000 and the something else.

Why is that?


Solution

  • Maybe you have another instance running (in the background, in other terminal) and listening on port 3000.

    To see what ports are open and by which process, run:

    netstat -ltnp
    

    To see what processes are running, run:

    ps aux | grep node
    

    or:

    ps aux | grep server.js
    

    Try shutting the program down and make sure nothing listens on port 3001 and then try to access port 3000.