listen EADDRNOTAVAIL error in Node.js

前端 未结 11 797
陌清茗
陌清茗 2020-11-27 15:43

I installed Nginx and Node.js in my server.

When I try run my node.js file, I get an error:

node.js:201
        throw e; // process.nextTick error, or \'e         


        
11条回答
  •  不知归路
    2020-11-27 16:07

    I had this issue, and I thought it was a port issue, but it turned out to be an IP issue.

    I had my HOST environmental variable pointing to a static IP, and my Node server was listening to that address. At some point when my router recycled, it started assigning my machine a new IP, and I started getting the error you posted. It was fixed by changing the address my server was listening to.

    My config:

    app.set('port', process.env.PORT || 3000);
    app.set('host', process.env.HOST || '0.0.0.0');
    
    http.createServer(app).listen(app.get('port'), app.get('host'), function(){
      console.log("Express server listening on port " + app.get('port'));
    });
    

    Make sure your host is hitting your machine. In any case, the loopback, as @miltonb suggested, should always work:

    app.set('host', '127.0.0.1');
    

提交回复
热议问题