[#NODEJS] Performing some routing with ExpressJs


Writing is not an easy task. Many seems to think that each post goes almost without thinking, when the reality is there a detailed plan involving the phrase, “what are you doing today for tomorrow’s post?“. And that’s it!, no more than 24 hours of planning for each post. The problem is that when you are dealing with many technologies at the same time, it becomes difficult to find references to explain some topics; and that is the reason for my today post: try explain a very fast way how to create a routing scheme with ExpressJs in NodeJs.

And as always I will share a step by step

1. Create a directory where the files from our NodeJs server will be stored. In my case is “E:\srcElBruno\Tests\NodeJs\Demo01\”

2. Open a Command Prompt and navigate to the previous location.

3 Install the Express package from NPM (Node Package Modules, all the information here). This is done with the command

NPM install express

4. Create a file named server.js in the root directory.

5 Add the following content within the same

   1: var express = require('express');

   2: var app = express();


   4: app.get('/', function(req, res){

   5:   res.send('hello world');

   6: });


   8: app.get('/elbruno', function(req, res){

   9:   res.send('la page de El Bruno);

  10: });


  12: app.listen(1337);

6. Now, we launched our server NODE with the command

node server.js

7. We have our app running! If you browse to the root of our server, on port 1337 (line 12), we will see the so classic as useless Hello World


If instead sail to the address http://localhost: 1337/elbruno will see a not so classic but not less vain “page of the bruno


8. If we want to give you a bit of intelligence (not many) could change this redirection to make it directly against my blog

   1: app.get('/elbruno', function(req, res){

   2:   res.redirect('http://www.elbruno.com');

   3: });

9. The complete reference of ExpressJs can be found here

Saludos @ Home

El Bruno

image image image Google

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.