Getting Started With Node.js

Junerey Casuga in backend, javascript, node

You may have heard about Node.js but you don’t know what exactly it does. To make it short and simple, Node.js allows you to run Javascript code in the backend.

Installing Node

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
sudo apt-get install python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

Hello World

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:

1
console.log("Hello World");

Save it and execute it using Node.js by running this command on your terminal:

1
node helloworld.js

This should print “Hello World” on your terminal.

Write everything

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.

Modules

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.

1
var http = require('http');

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:

1
var http = require('./yourModule.js');

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:

1
npm install module_name

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
var http = require("http");

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

Now, run this command using your terminal:

1
node server.js

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.

Routing

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
var http=require("http"),
  url=require("url");

http.createServer(function(request, response){
  var pathname = url.parse(request.url).pathname;
  console.log("Request for " + pathname + " received.");

  response.writeHead(200, {"Content-type":"text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);

Save it, and run

1
node server.js

. Open up your browser and navigate to http://localhost:8888.

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.

Conclusion

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.

This is just a preview of what you should know about Node.js. If you want to make your life easier, you might want to pick a framework for Node.js like Express and Partial.