Category: Tutorial

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 server.js

or

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

forever logs

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:

forever list

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

Back to Part 1

My Dev Env – remote access to a Raspberry Pi

You have three UI options for logging into your Raspberry Pi:
1. The “standard” way: connect a USB keyboard and HDMI monitor directly to the Pi.
2. connect a PC or Mac to the Pi by plugging a USB-to-serial console cable like the Adafruit 954 into the Pi’s GPIO header, and use the Terminal app on your laptop to connect and login to the Pi as described here.
3. connect your Pi to your local network and then use the SSH app on your Mac to open a remote session on the Pi as described here.
I  use all three methods, and each has its benefits in different situations.
Once I’ve initially setup my Pi for networking using option 1 or 2, I find the third option – connecting via LAN – most flexible, since it doesn’t require the Pi to be tethered to my Mac or any other equipment. I can power up the Pi on my workbench, and as soon as it’s connected itself to the WiFi, I can SSH into it from anywhere in the house. Or even over the Internet if I open a port in my router for the purpose.

Continue reading

REST API on a Pi, Part 2: control your GPIO I/O ports over the internet

In Part 1 of this series, we built a simple REST API in JavaScript on our Raspberry Pi.
One of the most useful reasons for providing your Raspberry Pi with a REST API is to expose its input and output ports via the Internet for remote monitoring and control. This will allow you to control your RPi’s inputs and outputs from the browser on any smartphone or PC wherever you are in the world.
So now we will do just this, extending our REST API implementation from part 1 to read and display digital input ports.

Continue reading

Javascript on a Raspberry Pi – How to install Node.JS

 

Why install Node.JS on a Raspberry Pi ? 

Nodejs_logo_lightI’ve been using Node.JS as the backend framework for building single-page web apps recently. On top of providing the advantage of an asynchronous, event-based programming model on the backend, it means I can code in Javascript on both the frontend and the backend again – just like in the good old days coding client-server applications in C.

Raspi-PGB001-300x267

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.

Continue reading

How to find the MAC address of your Raspberry Pi

You sometimes need to find out the MAC address of your Raspberry Pi. For instance, you want to find it among the list of devices attached to your LAN, because you want your router’s DHCP service to assign your RPi a permanently dedicated IP address instead of letting the DHCP just assign it the next available IP address each time you start it up. Then you can SSH into the same IP address each time to log into your RPi from your PC or Mac knowing it will always be there, without having to search for the RPi on the network.
Some LANs are configured to allow access only for a white-listed devices by MAC address, so you’ll also need to know yours to be able to use such a LAN.

Continue reading

How to build a REST Web API on a Raspberry PI in JavaScript

One of the most useful reasons for providing your Raspberry Pi with a REST API is to expose its inputs and outputs to a web client (on any iPhone, laptop or desktop PC anywhere in the world) for remote monitoring and/or control. This is part 1 of a 2 part blog showing how to implement a REST API in JavaScript.

Continue reading

A Node.JS Application on Amazon Cloud. Part 3: A simple Webserver in Javascript using Node, Express, and MongoDB

In this third part of our exercise, we’re going to use the primary AWS EC2 instance on which we installed Node in Part 1, and the database EC2 Instance  we set up in Part 2, as the platform for  building a simple web application server. We are going to code that web server in Javascript, using Node together with the Express and Mongoose Node library packages.

Continue reading