auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.1.20
netmask 255.255.255.0
gateway 192.168.1.1
network 192.168.1.0
broadcast 192.168.1.255
wireless-power off
auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
address 192.168.1.21
netmask 255.255.255.0
gateway 192.168.1.1
network 192.168.1.0
broadcast 192.168.1.255
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
Install dependencies:
$ sudo apt-get update
$ sudo apt-get install git-core
$ sudo apt-get install zsh
Install oh-my-zsh
$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Change current shell to zsh
$ chsh -s $(which zsh)
Restart Raspberry
$ sudo shutdown -r 0
- http://weworkweplay.com/play/raspberry-pi-nodejs/
- http://joshondesign.com/2013/10/23/noderpi
- http://www.quietless.com/kitchen/how-to-setup-node-js-on-a-raspberry-pi/
Install dependencies:
$ sudo apt-get upgrade
$ sudo apt-get update
Donwload Node.js
$ wget http://nodejs.org/dist/v0.8.19/node-v0.8.19.tar.gz
$ tar xvzf node-v0.8.19.tar.gz
$ cd node-v0.8.19
Build Node from source
$ ./configure && make && sudo make install
Check if node and npm is installed
$ node -v
$ npm -v
Cool, now let’s run a simple test app.
Create a new file and call it hello-node.js and paste the following into it:
// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
Now start the server
$ node ./hello.node.js
And navigate a browser to localhost:8000 and you should see “hello world” print to the screen.
First cd into /etc/init.d/ and create a new file called node-server.sh
$ cd /etc/init.d
$ sudo nano node-server.sh
Next copy the following into it:
Note to change the path_to_node_app variable to wherever your app lives.
#! /bin/sh
# /etc/init.d/node-server
### BEGIN INIT INFO
# Provides: node-server
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
# change this to wherever your node app lives #
path_to_node_app=/var/node/hello-node.js
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "* starting node-server * "
echo "* starting node-server * [`date`]" >> /var/log/node-server.log
/usr/local/bin/node $path_to_node_app >> /var/log/node-server.log 2>&1&
;;
stop)
echo "* stopping node-server * "
echo "* stopping node-server * [`date`]" >> /var/log/node-server.log
killall /usr/local/bin/node
;;
*)
echo "Usage: /etc/init.d/node-server {start|stop}"
exit 1
;;
esac
exit 0
Next we need to make this file executable via :
$ chmod 755 ./node-server.sh
Now let’s tell the PI to execute this script and start the server on reboot:
$ update-rc.d node-server.sh defaults
If we ever want to disable this just type :
$ update-rc.d -f node-server.sh remove
And lastly you can now start and stop your node server in the background anytime via :
$ sudo /etc/init.d/node-server.sh start
$ sudo /etc/init.d/node-server.sh stop
I personally added aliases to these two commands in my ~/.bashrc
alias nodestart='sudo /etc/init.d/node-server.sh start'
alias nodestop='sudo /etc/init.d/node-server.sh stop'
alias nodetail='tail -n100 /var/log/node-server.log'
So I can start / stop the server and view the log file simply by typing the following :
$ nodestart
$ nodestop
$ nodelog