更改 Node.js 侦听端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12181253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:14:19  来源:igfitidea点击:

Changing Node.js listening port

node.js

提问by jim dif

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

我刚刚在 Windows 上安装了 node.js。我有这个不运行的简单代码:

I get: Error: listen EADDRINUSE

我得到:错误:听 EADDRINUSE

Is there a config filethat tells node.js to listen on a specific port?

是否有一个配置文件告诉 node.js 侦听特定端口?

The problem is I have Apache listening on port 80 already.

问题是我已经让 Apache 监听了 80 端口。

EDIT:

编辑:

var http = require('http'); 
var url = require('url'); 

http.createServer(function (req, res) { 
 console.log("Request: " + req.method + " to " + req.url); 
 res.writeHead(200, "OK"); 
 res.write("<h1>Hello</h1>Node.js is working"); 
 res.end(); 
}).listen(5454); 
console.log("Ready on port 5454");

回答by Mike Christensen

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:

除非您自己创建配置文件,否则没有配置文件。但是,端口是listen()函数的参数。例如,要侦听端口 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.

查看每个适配器正在使用的所有端口的列表。

回答by maudulus

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

我通常在app.js文件中手动设置我正在侦听的端口(假设您正在使用express.js

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

This will log Ready on port 8080to your console.

这将登录Ready on port 8080到您的控制台。

回答by Gaurav Singh

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

你可以从http://nodejs.org/获取 nodejs 配置
你需要记住的重要事情是它在文件 app.js 中的配置,它由端口号主机和其他设置组成,这些设置对我有用

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

在这种情况下,如果您收到“错误:侦听 EADDRINUSE”,请更改端口号,即,这里我使用的是“49165”,因此您可以使用其他端口,例如 49170 或其他端口。为此,您可以参考以下文章
http://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-shared-hosting-accounts