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?