Changing Node.js listening port

后端 未结 3 712
名媛妹妹
名媛妹妹 2020-12-06 04:34

I just installed node.js on Windows. I have this simple code which does not run:

I get: Error: listen EADDRINUSE

Is there a config file that

相关标签:
3条回答
  • 2020-12-06 04:56

    There is no config file unless you create one yourself. However, the port is a parameter of the listen() function. For example, to listen on port 8124:

    var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
    }).listen(8124, "127.0.0.1");
    console.log('Server running at http://127.0.0.1:8124/');
    

    If you're having problems finding a port that's open, you can go to the command line and type:

    netstat -ano
    

    To see a list of all ports in use per adapter.

    0 讨论(0)
  • 2020-12-06 05:00

    I usually manually set the port that I am listening on in the app.js file (assuming you are using express.js

    var server = app.listen(8080, function() {
        console.log('Ready on port %d', server.address().port);
    });
    

    This will log Ready on port 8080 to your console.

    0 讨论(0)
  • 2020-12-06 05:08

    you can get the nodejs configuration from http://nodejs.org/
    The important thing you need to keep in your mind is about its configuration in file app.js which consists of port number host and other settings these are settings working for me

    backendSettings = {
    "scheme":"https / http ",
    "host":"Your website url",
    "port":49165, //port number 
    'sslKeyPath': 'Path for key',
    'sslCertPath': 'path for SSL certificate',
    'sslCAPath': '',
    "resource":"/socket.io",
    "baseAuthPath": '/nodejs/',
    "publishUrl":"publish",
    "serviceKey":"",
    "backend":{
    "port":443,
    "scheme": 'https / http', //whatever is your website scheme
    "host":"host name",
    "messagePath":"/nodejs/message/"},
    "clientsCanWriteToChannels":false,
    "clientsCanWriteToClients":false,
    "extensions":"",
    "debug":false,
    "addUserToChannelUrl": 'user/channel/add/:channel/:uid',
    "publishMessageToContentChannelUrl": 'content/token/message',
    "transports":["websocket",
    "flashsocket",
    "htmlfile",
    "xhr-polling",
    "jsonp-polling"],
    "jsMinification":true,
    "jsEtag":true,
    "logLevel":1};

    In this if you are getting "Error: listen EADDRINUSE" then please change the port number i.e, here I am using "49165" so you can use other port such as 49170 or some other port. For this you can refer to the following article
    http://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-shared-hosting-accounts

    0 讨论(0)
提交回复
热议问题