CGI version of expressjs - node.js

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:
Use ProxyPass to forward requests from Apache to node.js
Use Rewrite rules in .htaccess file
Use VPS
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.

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.

Related

What would be the recommended way to exchange data from a WebGL application running on a Raspberry Pi to write to the Pi's serial interface?

Here's the full problem:
I have a WebGL build of a Unity project running on a Pi, and I need the data from this process written to the Pi's serial interface (and vice versa). The best possible solution I can think of is hosting the WebGL server using Express, and utilizing nodejs for the backend process of somehow running some python code that works the serial interface. Problem is, I'm super inexperienced with these platforms and I don't have enough familiarity with how they all work together to intuitively feel out what to do. Hoping I can get some guidance on any recommended paths.
Pre answered questions:
Unity project on a Pi?: I have a simulation running in Unity, and I need it running on a lightweight computing platform. The only way to run this on a Pi is in a WebGL build.
What are you trying to achieve?: I'm trying to achieve a communication pipeline with this WebGL process and an Arduino, specifically over serial.
What I've tried:
I've tried running the WebGL process utilizing systems like Banyan, which would allow communication with a server, but it seems the WebGL build does not allow sockets.
I've tried hosting the server using Express through nodejs, and the app does actually work, I just need help understanding how this system works such that I can exchange data from the WebGL app and a python script that is running the serial comms. As stated above, I'm very new to the nodejs system so I may not even be using the nomenclature properly.

Can i run a second web server?

So i own a raspberry pi server which runs a python web server i want to run a second web server on the pi but im not sure if it can handle it.
Raspberry pi 2 model B,
Python 3,
Ubuntu Software,
Short answer: Yes, you can.
Long answer: It depends on what your web servers will be doing and how good your internet is. I have a RasPi 4 running OctoPrint, a few bots and some other misc. stuff. Also, you may run into problems along the lines of your web servers trying to listen for connections on the same port, which is another problem outside the scope of your initial question.

How do I host a NodeJS http web server on a raspberry PI/or any linux based PC

I've seen people host Apache servers on raspberry PI's and of course on regular computers. I do not wish to or rent a VPS and thought it would be best to set one up on my Raspberry PI 2 B:
(1gb ram/32gb disk/1ghz on overclock)
What is the proper way of setting up a NodeJS http web server on a computer? Can you link me to a tutorial if one exists(Couldn't find any).
I was also wondering if I would be better of renting a VPS with recurring costs and lack of customization/control , or on a working dedicated PC. Keep in mind that I wish to practice some dev ops work over multiple PI's(ex. Chef).
I need this server to be accessible through the internet through an ip or domain. How do I go about setting that up? (I have domains)
Update: I wrote a blog post on this subject.
The tutorial goes through the full installation process. Click the link below
Yes, You can use raspberry as your VPS or dedicated server. Just assign static IP address(You need to buy) to your Raspberry and then point your domain name to that static IP address.
Few links below that will be helpful for you,
https://www.modmypi.com/blog/how-to-give-your-raspberry-pi-a-static-ip-address-update
https://in.godaddy.com/help/point-your-domain-name-to-a-server-19116

communicate with my raspberry PI, through my webserver

I want to communicate with my raspberry pi, through my webserver.
I want to use a web interface on my server, with which I can control an LED on my PI.
Can I use "node js" for this? or does anyone have a good idea or examples?
regards
You can indeed use just about any web server to communicate with the pi and thereby control its GPIO pins.
I wrote a web server specifically to interface with the pi's GPIO capabilities complete with utilities and examples if you want to try it out. It's a very lightweight native-code (C++) web server that you can use to control your LEDs (or what have you) with about 5 mins of setup:
OliWeb on GitHub
You can install it using git with:
sudo git clone https://github.com/m2ware/OliWeb.git
You could also install just about any other web server out there (Nginx, Node.JS, take your pick) and set up CGIs to call command-line utilities to drive the LED pins. Gordon's WiringPi utilities are easy to use and install - installation and usage instructions are below.
Gordon's WiringPi Utility
Each web server will have its own particulars in terms of invoking command line interfaces via CGI. If you're interested in NodeJS specifically, this describes how to invoke command-line functionality from Node:
How to invoke external scripts programs from node js
I know it is a little bit late, although for those who may be still interested I've recently developed a school project which does exactly this job.
I've used WebSocket and GPIO handling.
Here's the repo for the client: https://github.com/jkomyno/material-led-controller/
Here's the one for the server (you gotta put this on your RPI):

Local pi server running node and websockets

I'm just wondering is it possible to run a local server using node (node would be running on a raspberry pi) and communicate with it via a web browser on a different machine on the same network using websockets? I'm looking into using a browser as a control system for a project I'm working on. If it is possible is there any references I can look at?
Thank you,
Techhead55.
EDIT
I already have node up and running on the pi, it's just that I'm not sure if you can and how you implement a local server and use sockets to communicate with it in a bowser on the same network.
Not sure if you specifically ask about Node, but here is an example of a WebSocket server running on Pi, and browsers connecting to that server: http://tavendo.com/blog/post/pypy-on-the-pi/
The example does Publish & Subscribe over WebSocket using WAMP (http://wamp.ws).
Here is a similar example running on Arduino Yun that shows how to connected hardware, and publish data in real-time via WebSocket, receiving data in a browser, and real-time rendering a chart: http://tavendo.com/blog/post/arduino-yun-with-autobahn/
Disclosure: I am original author of Autobahn and work for Tavendo.
Yes.
Here is great manual from Matthias Rüedlinger:
http://blog.rueedlinger.ch/2013/03/raspberry-pi-and-nodejs-basic-setup/
Also installation of ghost with node on RPI is very nice :)
http://bit.ly/1cFwFAQ

Resources