0

I want to create a small web service and for that I really want to use node.js and express.

I already have an Apache running on my little server (it's a Raspberry PI :P ) which receives all the traffic. How can I configure a VirtualHost to be served by node.js?

I searched a lot and I found many solutions, namely:

But all these solutions require a running daemon for node.js and resources are scarce on Raspberry PI (it only has 256MB of RAM). So I searched a bit more and I found this solution:

GGI-Node provides the ability to run JavaScript on any web server just like PHP as a CGI using Node.js.

But again, this solution doesn't fulfill my requirements. GGI-Node is more of an adaptation of node.js to PHP and it's not possible to use with/like express.

So I came up with an idea: what about modifying express to keep the same behavior and functionality and, instead of creating a server, feeding express with info from Apache via CGI? I searched even more and I didn't find anything like that... Is this possible to do? How hard would it be to implement?

I know this solution doesn't take the advantages of node.js asynchronous I/O, but it's not important in my case.

Community
  • 1
  • 1
rigon
  • 1,310
  • 4
  • 15
  • 37
  • 1
    This would be quite a major endevor to undertake (in my oppinion). At that "cost", you would be better off buying something more powerfull that is capable of running your app. Like a Odroid board or something like that. – Mathieu de Lorimier Dec 08 '16 at 21:33
  • Yes, makes sense. – rigon Dec 08 '16 at 21:44

1 Answers1

1

Here's one possible solution: Get another Raspberry Pi or two :-)

No, I'm not kidding. Here's what I'm thinking:

With one additional Raspberry Pi:

Set up the Raspberry Pi running Apache as a proxy for your new virtual host & route all the traffic for that virtual host to Raspberry Pi #2. Set up node.js and your express application on Raspberry Pi #2.

With two more Raspberry Pis:

Leave Raspberry Pi #1 as is (with Apache & whatever other web site you have running on it).

Set up Raspberry Pi #2 with node.js & your express application

Setup up Raspberry Pi #3 with a proxy and proxy requests to #1 & #2. Apache can do it, although it's a pretty "heavy" solution - I'd go with nginx personally.

Actually, you could do it with just one Pi

Set up the Express app to listen on some arbitrary port & use Apache to proxy requests to the new virtual host to that port.

However...

How ever you do this, be advised that the Raspberry Pi (at least the earlier models) are not good at running node.js. I tried this (running an Express app) with an original Raspberry Pi and performance was not good (I wound up rewriting the code in go). You might have better luck with a Pi 2 or a Pi 3.

If you decide to go this route...

Here's a link to a Digital Ocean tutorial on using Apache as a proxy. I haven't followed this tutorial, so I can't vouch for it, but a cursory glance suggests that it's pretty complete.

Kryten
  • 15,230
  • 6
  • 45
  • 68