Making a simple web server with Node.js
One of the most important aspect of having a Raspberry Pi is that we have a real computer configured with services and servers. In this section, we will explain how to install Node.js, which is a powerful framework that we will use to run most of the applications we are going to see in this book. Luckily for us, installing Node.js on Raspberry Pi is really simple.
In the folder for this chapter, open the file called webserver.js
. We will create a server on port 8056. To test the program and see the results we have to open the Node.js terminal on your MS-DOS interface and run this file with the following command:
node webserver.js
Add the following lines to webserver.js
file to declare the HTTP request commands:
var http = require('http');
We create the server with the following function:
http.createServer(function (req, res) {
We define the content of the file that we will show in the HTML code:
res.writeHead(200, {'Content-Type': 'text/plain...