A Node.JS Application on Amazon Cloud. Part 4: Launching your webserver
And finally, launch your webserver for continuous operation
When you’re ready to launch your webserver and leave it running, you need to start it as a background process.
If you don’t, then when you logout or close your SSH session, you inadvertently kill your webserver because you ran it in the foreground – ie as a child of your SSH process. To avoid this behaviour you could of course simply run the server in the background by appending & to your bash command:
node server.js &
However, the node server may still die when you close your SSH session, because even though the node process is running in the background, it’s standard console outputs stdout and stderr are still pointed at the terminal. That means that if the node server tries to write a message to console.log or console.error it will encounter a pipe error and terminate. This can be avoided by piping the output of your process to logfiles:
node server.js > stdout.log 2> stderr.log &
You will also want to log in later and review the contents of the logfiles to diagnose any problems you may get using your API.
If you still get problems then you can try standard commands like nohup, which can be used to run all types of processes in the background.
Or try this solution developed specifically for Node, which has a number of advantages :
First, install the forever node package using npm:
sudo npm install -g forever
And then start your application:
forever start server.js
You don’t need to pipe stdout and stderr to files now, forever itself writes any output to these streams to log files.
you can define the log file location and names using the -o and -e command line arguments, or just use the default. To find the location of the log files forever has created, use the command
An added benefit of using forever is that forever can automatically restart your app when it crashes or stops for any reason. To restrict the number of restarts to, say, 10 you could use:
forever -m10 server.js
forever has a number of other useful commands, such as :
To list all running processes:
Then, to stop a process, take a note of the process id number within the brackets and use it as following command:
forever stop 0
Restarting a running process goes:
forever restart 0
If you’re working on your application file, you can use the -w parameter to restart automatically whenever your server.js file changes:
forever -w server.js
In the days when processing power was expensive, writing assembler code was the benchmark, and high level languages were seen as an extravagance, C was a revelation. Near-assembler fast, bit-level operations, but still an expressive 3GL.
Wait, that’s only the beginning of the story.
REST API on a Pi, Part 2: control your GPIO I/O ports over the internet
Event-driven programming, Finite State Machines and NodeJS
I started out in my software engineering life writing software for embedded real-time systems and communication stacks, so I often think of myself as an async native. I’ve written a lot of code in various languages, mostly not using event-driven programming models and design patterns, but hardware interrupts, callback functions, event loops and finite state machines – I still see these as reassuringly familiar features of my original habitat. Call it the baby duck syndrome.
Who is this post for?
How to Install Node.JS on your Mac OS X
Why install Node.JS on a Raspberry Pi ?
And so when I come up with an application where I want to use my Raspberry Pi as a micro web server, but one that needs more than the ability to serve static webpages, I right away think of Node.
a Raspberry PI NFC Reader
Coming soon – how I built an NFC card reader on the Raspberry Pi with a web dashboard displaying NFC events detected in real time…