更改Node.js监听端口 [英] Changing Node.js listening port
本文介绍了更改Node.js监听端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚在Windows上安装了node.js.我有一个无法运行的简单代码:
I just installed node.js on Windows. I have this simple code which does not run:
我得到: 错误:听EADDRINUSE
I get: Error: listen EADDRINUSE
是否有一个配置文件告诉node.js在特定端口上进行侦听?
Is there a config file that tells node.js to listen on a specific port?
问题是我已经让Apache监听了端口80.
The problem is I have Apache listening on port 80 already.
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");
推荐答案
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.
这篇关于更改Node.js监听端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文