As I stated, Node.js can be used to build a web application. We can use the HTTP library to build a simple web server. Let's try to write the following code:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Node.js'); }).listen(8056); console.log('Server running at port 8056');
Save this code into a file named mywebserver.js
. The http.createServer()
function is used to create a web server with a specific port. In this case, I used port 8056.
Now you can run this file. Type the following command:
$ node mywebserver.js
To test this program, open a browser and navigate to http://<ip_address_raspberry_pi>:8056
. You should see Hello Node.js in your browser as follows: