NodeJS HTTP - listen on other port than 80

后端 未结 6 919
情歌与酒
情歌与酒 2021-01-12 00:21

I am running XAMPP on Windows to host an Apache server on port 80. Now I\'m trying to have a NodeJS script running in the background but the problem is that it can only list

6条回答
  •  我在风中等你
    2021-01-12 01:22

    I haven't really understood what you meant by you're not getting any response, because I ran the same code and it works fine for me.

    I only noticed something here (I kept a note for you in the comment)

    server = http.createServer( function(req, res) {
        if (req.method == 'POST') {
            var body = '';
            req.on('data', function (data) {
                body += data;
                doStuff(body); //you haven't defined doStuff so you will end up with a error message on this line, but you sever will still run fine
            });
            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');
        }
    
    })
    

    When running your post request, don't forget to add "" in your body area, select raw then choose JSON(application/json). That should run fine for you, except you might get a reference error as shown below, but you should still get your response of received request successfully.

    error

                doStuff(body);
                ^
    
    ReferenceError: doStuff is not defined
    

    Ensure that you're doing the same thing and let us know if your issue it resolved.

提交回复
热议问题