我在Windows上运行XAMPP来托管一个在80端口上运行的Apache服务器。现在我正在尝试让一个NodeJS脚本在后台运行,但问题是它只能监听80端口。如果这样做,一切都按照预期工作,但我不能同时运行Apache,因为Apache具有优先权并仅提供我的网站服务。NodeJS脚本甚至无法开始监听。
我的问题是:如何切换NodeJS脚本的监听端口(具体端口并不重要),以便Apache仍然可以在80端口上运行,我可以从世界各地访问NodeJS脚本。
NodeJS代码的一部分:
const http = require('http');
const port = 8080;
const host = '0.0.0.0';
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
doStuff(body);
});
res.writeHead(200, {'Content-Type': 'text'});
res.end('received request successfully');
}
else {
res.writeHead(405, {'Content-Type': 'text'});
res.end('not allowed method ' + req.method + ', try again with GET or POST');
}
})
server.listen(port, null, function(error){
if(!!error){
console.log("\x1b[41m%s\x1b[0m", "error while initializing listener on port " + port + ": " + error);
}
else{
console.log("\x1b[32m%s\x1b[0m", "started listener at 'http://" + host + ':' + port + "'");}
});