This great thread explains, how to use ProxyPass in an Apache Server to have everything that matches the route /node
forwarded to port :8080, where there's a Node JS server running:
Apache and Node.js on the Same Server
Now I wonder whether there is a similar way do do this the other way around.
There are NodeJS servers running on both :80 and :8080. However, if the route matches /blog
, it should instead show my wordpress installation.
Due to a business guy setting up our domain system (argh), this is the only option I can think off - subdomains won't work.
You can always have NodeJS make internal HTTP requests to your running Apache service (socket), based on the URL passed by your NodeJS http listener; then just feed the result back through the http response accordingly.
Here's an excellent post on how to make http requests with NodeJS: https://davidwalsh.name/nodejs-http-request
Synopsis
So, if you have Apache running on i.e: IP & port (socket) 127.0.0.1:4321
you can have NodeJS relay requests for Apache like this: (just an example)
var http = require('http');
http.createServer(function (nreq, nrsp)
{
if (nreq.url.indexOf('/blog') > -1)
{
http.get('http://127.0.0.1:4321', function(arsp)
{
arsp.on('data', function(data)
{
nrsp.write(data);
});
});
}
else
{
// your nodeJS web server stuff here
}
}).listen(8080); // or port 80, but this requires root privilege
While it is possible I really wouldn't recommend this.
Webservers (like Apache) are very good at being Webservers. They have numerous security and performance options built into them. They are good with static content. What they are not good at is running dynamic code (they mostly delegate to PHP or other CGI scripts or proxy pass to specific servers like Node). Node is the reverse.
Node can create a web server of course and has a great community around it constantly working on performance and security and new features. But that does not mean being the primary web server is necessarily a good idea - especially when you have to install a "real webserver" (pardon the phrasing Node developers!) in front of it anyway.
For Node you basically have to write the code to create the webserver (using libraries like Express of course). For Webservers the code is written and you have to write config files. A subtle distinction maybe but an important one with numerous benefits (syntax checking of config, tried and trusted solution, documentation on all config options... Etc.).
In addition Node is very good at creating micro services - so having many node services listening on different ports, and then having a webserver in front to bring them all together and/or handle https termination as well. Yes you could have one node server in front performing that function, instead of Apache, but I would still recommend a webserver for this instead.