0

I want to replace the PHP script that currently handles receiving and processing data from my webpage (LAN only) by a NodeJS file. Currently, the JSON data is being sent in JS with an XMLHttpRequest:

var xhttp = new XMLHttpRequest();
var url = "/server.php";
xhttp.open("POST", url, true);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.onreadystatechange = function () {
    ...
};

xhttp.send(content);

Obviously, server.php is the file I'm looking to replace. In this file, I receive the data like this:

$stringHttp = file_get_contents("php://input");

I have searched far and wide on how to do something like this in NodeJS, but everything I find uses this basic layout:

http.createServer((request, response) => {
    ...
}).listen(8091);

Now, since my webpage is hosted by Apache, it's probably not possible to create this server on the same port. At least, that's what I'm getting from the error message I get when I try to run the NodeJS file:

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::8091
    at Object._errnoException (util.js:992:11)
    at _exceptionWithHostPort (util.js:1014:20)
    at Server.setupListenHandle [as _listen2] (net.js:1355:14)
    at listenInCluster (net.js:1396:12)
    at Server.listen (net.js:1480:7)
    at Object.<anonymous> (/var/www/apache/testNode.js:15:4)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)

So basically, I'm looking for a NodeJS replacement of file_get_contents("php://input"). Hence my question: Can you obtain POST data in NodeJS without creating a server?

Timmiej93
  • 1,328
  • 1
  • 16
  • 35
  • You could run your node.js server on a different port and change your request path to something like `/node/server` and then configure your Apache server to proxy any requests that start with `/node` to your node server on its port. – jfriend00 Jul 29 '18 at 23:48

2 Answers2

2

No, it isn't.

In your PHP version, you have a server. It is Apache and it makes use of (for example) mod_php to execute the PHP.

If you are executing your program with Node.js, then you need some way to get the HTTP request to the program. That involves running a server.

it's probably not possible to create this server on the same port

No. You'd need to run it on a different port. (And then either post to it directly or configure Apache to act as a proxy in front of it).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • `You'd need to run it on a different port. (And then either post to it directly`. That's exactly what I did, and it works perfectly. Thanks for the tip! – Timmiej93 Jul 30 '18 at 21:17
0

Well, if you really just want to run a node.js script one and done to get a result, then you could keep a shell of your PHP script and have it run node and your script. You could either pass the node script the required data on the command line or you could send it in stdio and the node script would grab it from whichever.

Your node script would run and create the desired result and write it to stdout. The PHP script would then grab the stdout data and forward it as the http response.

Nobody is going to describe this a super optimal way to do things. HTTP response is sent to Apache, then fires up the PHP interpeter to run your PHP script which fires up node.js to run your node.js script. But, if it's a one-off thing just for this one use and there's some compelling reason that you use PHP elsewhere and need node.js for this one thing, then you could make it work.

It may even be possible to create a custom path for this one script that Apache could be configured to detect and then run your node script directly (like it does for PHP) without the PHP middleman. I don't know Apache well enough to advise exactly how to do that, but there are some references on doing it. Again, not optimal, but it could be made to work.


The best performance solution would be to actually create a node.js server on another port and have either Apache or some other proxy detect certain requests (usually based on the path) that you want redirected to your node.js server rather than sent through to PHP or post directly to the other port from the client (you'd have to enable CORS in your node.js server to allow that to work).

jfriend00
  • 683,504
  • 96
  • 985
  • 979