Getting Started With Node.js
Before we start on writing an app, we need to install it first in our machine. For MacOS and Windows, there’s a pretty straight forward installers which you can find on nodejs.org. For Linux, you can run these lines of command on your terminal:
1 2 3 4
We have node on our machine now. So let’s proceed on writing our first node.js app, our ever loving “Hello World”.
Fire up your favorite text editor and create a file called helloworld.js. Now, we want node to write “Hello World”. So put these code on helloworld.js:
Save it and execute it using Node.js by running this command on your terminal:
This should print “Hello World” on your terminal.
Unlike PHP, you have Apache that can receive HTTP requests and serve web pages. With node, you also have to implement your own HTTP server.
It may sound difficult, but it’s not. Thanks to the modules that came with Node itself. I will show how to write your own HTTP server later.
Node.js has module to make your life (somewhat) easier. Modules are encapsulated piece of codes. Means that a module contains functions related to the subject of the module. Let’s just take the ‘http’ module as an example. This module, obviously, contains functions specific to HTTP. So how do we use these modules? Just use the require function.
Other than the module that came along with Node, you can also write your own module and you can include it to your app by:
You can also install new modules with the help of Node Package Manager(NPM) which came along with Node.js as you install it. To install new modules using NPM, fire up your terminal, navigate to your app directory, and run this command:
Writing your HTTP Server
As I promised, we will write our own HTTP server using Node.js. Create a new JS file named server.js and put this codes inside the file.
1 2 3 4 5 6 7
Now, run this command using your terminal:
Fire up your browser and navigate to
http://localhost:888. Your browser should be able to run a page displaying the text “Hello World”.
Sweet! So let me explain what we have written.
The first line requires the http module that comes with Node.js and makes it accessible throught the http variable.
Then we called one of http module’s function which is the createServer function. This function returns an object and this object has a method listen. The listen method accepts a numeric value which indicates the port number the HTTP server is going to listen on.
And inside the function we used the response.writeHead() function to send a HTTP status code(200, means success) and a content-type in the HTTP response header. The response.write() function sends the “Hello World” text to the HTTP response body. And the write.end() ends the response.
Now, we also need to handle URL, GET, and POST parameters into our router. We may need to include the url and querystring to able to do this.
The url module allows us to extract different parts of a URL.
The querystring module parses the body of a GET or POST requests.
But we will concentrate on using the url module for now.
1 2 3 4 5 6 7 8 9 10 11
Save it, and run
. Open up your browser and navigate to
As you run it through the browser, you should be able to see the requested url on your command line.
So, we just included the url module and made it accessible through the url variable. Then we parsed the request and passed it to pathname variable.
That’s it for now. I’m also new to Node.js and this is just what I’ve learned over the weekend. I just want to share it to you. Node.js requires extra effort but creating a powerful app with Node.js is worth it.