How to create a super simple Hello World application in Node.js

Before you begin please install Node.js on your computer by following the instructions in our How to install Node.js lesson.

Once you have installed Node.js open up your text editor and create a file called server.js and fill it with the following code:

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);

 

That’s it! You just create a Hello world application in Node.js.  But what is special about this application is that it is also a web application.  To test this first execute your Node.js script by typing in the following command in a terminal.

node server.js

 

Then open up a browser and type the following address.

http://localhost:8888/

 

You should see a web page that displays “Hello World”!

3-21-2015 7-42-03 AM

 

What is special about this little script is that you have created not only a little program that run in a browser (a web application) but it also the web server (HTTP server) to support this application.  A web server is the engine that powers all websites on the Internet. Now you have one powering your program!

3

Leave a Reply