Built.io Blog

Say 'hello' to node express

,

Today node.js is making a splash around the world. It's just another scripting language, so what makes this one so popular?

Non blocking I/O!

This makes node.js faster than any other scripting language. It is nothing but simple JavaScript, but it runs on the server.

JavaScript executes on the browser, but node.js runs on the server making use of Google's V8 VM, the same runtime environment for JavaScript that Google Chrome uses.

Getting Started

Start by installing node.js on your machine from the official site.

Note: For window users please install node version 0.8.15 as it is a stable node version right now.

Node.js already has a lot of useful modules and libraries, so you don't have to write everything from scratch.

HTTP Module

Start by using the "HTTP" module:

Create a file named server.js and paste following code in it.

var http = require("http"); 
<span style="font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace;font-size: 13px;font-style: normal;line-height: normal">http.createServer(function(request, response) {</span> 
<span style="font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace;font-size: 13px;font-style: normal;line-height: normal">response.writeHead(200, {"Content-Type": "text/plain"});</span> 
<span style="font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace;font-size: 13px;font-style: normal;line-height: normal">response.write("Hello World"); </span> 
<span style="font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace;font-size: 13px;font-style: normal;line-height: normal">response.end(); })</span>.listen(3000); ); 

You just created a HTTP server which runs on port 3000.

The first line requires the HTTP module that comes with node.js and makes it accessible through the variable http.

The next step is to call one of the functions the HTTP module offers: createServer. This function returns an object, and this object has a method named listen. It takes a numeric value which indicates the port number on which our HTTP server is going to listen.

Run this code by following command in schell

node server.js, then open your browser and point it at http://localhost:3000. This should display a web page that says Hello World.

You have just executed a universal begginers example with pure node.js.

Modules in Node

Now we will see how to use modules in node.

Let's install express module, by using the following command:

npm install -g express 

npm stands for Node Package Manager.

Normally, we use npm for the following purposes:

  • Publishing node application/library/module
  • Installing a node module in our system or node application
  • Managing dependencies of modules

Learn more about npm on the npm website.

-g is used for installing express globally so it will be available from any location from our machine.

About Express

Express is a flexible node.js web application framework. It is the most popular framework for node.js.

Create one express app by executing the following command:

D:>express hello-world 

See, you can access express globally.

You will see following response.

 
create : hello-world  
create : hello-world/package.json  
create : hello-world/app.js  
create : hello-world/public 
create : hello-world/routes 
create : hello-world/routes/index.js  
create : hello-world/routes/user.js
create : hello-world/public/javascripts  
create : hello-world/public/images 
create : hello-world/public/stylesheets  
create : hello-world/public/stylesheets/style.css 
create : hello-world/views  
create : hello-world/views/layout.jade  
create : hello-world/views/index.jade  
install dependencies:  $ cd hello-world &&npm install 
Run the app:
$ node app 

Now that you've created your first app, take a look at the directory structure of your app:

 
hello-world (root directory) 
  public (public directory) 
    javascripts (all client side executable javascripts files of app should reside here) 
    stylesheets (all style sheets of app should reside here) 
  routes (application logic resides here) views (presentation logic resides here)
  views (presentation logic resides here) 
  app.js (this is entry point for our app also routes http traffic) 
  package.json(contains json object of modules and it's dependencies) 

Go into app root directory: cd hello-world

After going into the app root directory, hit this command:

npm install 

This will install the depencies listed in package.json which was created during app creation. After running this a new directory named node_modules, it will install all dependent modules.

Run your app with the following command: node app

By default it will display the default node template.

NOTE: Node.js uses jade as the default template engine. We can configure this for HTML or any other supporting templating engine. We want to display Hello World string rather than this default template.

Go into /router/index.js and replace following line,

res.render('index', { title: 'Express' }); 

with this:

res.send('Hello World'); 

Here I purposefully did not use an app template to display Hello world is a different conversation.

To learn more about jade, visit jade-lang.com.

The next step is to open your browser and point it at http://localhost:3000. This should display a web page that says Hello World.

This should give you a basic idea about what node.js and express.js can do, and provide you with enough to get started on your own.

Learn more about node express by visiting expressjs.com.

Subscribe to our blog