Saturday, December 21, 2013

"Hello World" Node.js on Raspberry Pi

This post create a Node.js code on Raspberry Pi, to run a http server on port 8081, it can be visit by other PC.
"Hello World" Node.js on Raspberry Pi
"Hello World" Node.js on Raspberry Pi
To create a "Hello World" of Node.js, create hellonode.js, enter the code:
var http = require("http");

var server = http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World of Node.js, on Raspberry Pi");
    response.end();
});
server.listen(8081);

This code setup a http server to monitor port 8081, using node.

Run node in Terminal:
$ node hellonode.js
or
$ node hellonode

From another PC in the network, open a browser and vist <Pi IP address>:8081 to visit the node's http server.



Related: Install Node.js on Raspberry Pi

No comments: