NodeJS-使用80以外的其他端口进行公共访问

时间:2018-11-22 07:41:20

标签: javascript node.js http portforwarding

I have asked this question before, but I still haven't got an answer so I'm asking again.

我想用NodeJS托管多个http服务器,这些服务器都获取和发送http请求。同时,我想运行占用端口80的Apache。如果我禁用Apache并让NodeJS在端口80上运行,它将可以工作,但不能同时运行它们。

该脚本将在本地端口8081上运行并接收请求,但是即使使用路由器转发端口后,我似乎也无法通过Internet向其发送AJAX请求:

const http = require('http');

const port = 8081;
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');
        console.log("        \x1b[41mgot unexpected request '" + req.method + "' by '" + ipaddr + "' - aborting with status 405\x1b[0m\n");
    }

});


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 + "'");}
 });

0 个答案:

没有答案