Friday, February 7, 2014

Restful web services in node.js

In my latest blog restful web services with jetty and jersey I was trying to create rest web services simple as node.js, so I decide to show you how simple is to write this services with node.js.

First you will need node installed, you can install it from here nodejs.org, I'm going to use the express framework (expressjs.com) for node to help me develop the services, so to install express just go to the folder in which your projects will reside and with the help of the npm (node packaged modules) type the following command:

                 [jluna@host projects]$ npm install express

With this the express framework will be installed locally at your projects folder, now you can start creating the restful web services, first create a file in the projects folder for example services.js.

To use express use the following:


Once imported express to create a service just use one of its methods like this:

Finally, just configure the port and listen:

                 app.listen(process.env.PORT || 9999);

So here it is the whole script that contains the services:



As you can see the first service receives a parameter, this is specified in the path '/employee/getEmployee/:name', and to obtain the value of the parameter just get it from the request:

                  var name = req.params.name;

In javascript to create objects you don't have to define a class like in java just specify its properties when the object is created:

                 employee = new Object();
                 employee.name = name;
                 employee.age = 25;
                 employee.deparment = "HR";
                 employee.wage = 15000.00;

This has its benefits and its risks but I won't get into there, also there are other ways in javascript to create objects, you can define a function.

Finally to send the response to the client the json() method is used to parse the objects to json:

                 res.json(employee);

In the post method the interesting thing to see is that it is receiving an object, to access this object just get it from the body

                 var employee = req.body;

And then you can access the object like this:

                 console.log("Got name: " + employee.name);

Well to run the script just use the following  command in the path where the script is:

                 [jluna@host projects]$ node services.js

And the script should be up and running.

To test the script you can use the following commands:

curl -i -X GET -H 'Content-Type: application/json'  http://localhost:9999/employee/getEmployee/James

curl -i -X POST -H 'Content-Type: application/json'  -d '{"address": {"city": "Massachusetts","state": "Springfield","street": "Evergreen","zip": "66450"},"age": "25","deparment": "HR","name": "James","salary": "0.0"}'  http://localhost:9999/employee/postEmployee

In the console you should see the output, also here it is a java client you can use:


As you can see is very simple to create the services in node.js.

8 comments:

  1. Thank you. This helps and I understand it

    ReplyDelete
  2. http://andrewkelley.me/post/do-not-use-bodyparser-with-express-js.html

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. Node.js Tools for Visual Studio are accessible under the Apache open source permit. Microsoft proceeds with it to be an early alpha discharge, so it still has some harsh edges, yet the early surveys are extremely positive. The devices were created by the same group that likewise brought Python backing to Visual Studio and will work in both the 2012 and 2013 versions of the IDE.

    ReplyDelete
  5. while using this code
    //app.configure(function(){
    // app.use(express.bodyParser());
    //});
    it gives error

    ReplyDelete
  6. Nice post. Helpful in creating self-contained micro-service

    ReplyDelete
  7. The casino was the first to break ground at the Wynn in
    The Wynn 의정부 출장마사지 casino opened 남원 출장안마 in 1996 after a $3.8 billion expansion. 충주 출장마사지 It's part of 천안 출장안마 the Wynn Resorts empire. 광명 출장마사지 The casino opened in

    ReplyDelete