Restful Webservice with NodeJs in 5 Min
Its quite easy to create a restful web service for testing and during rapid development. Creating a quick prototype or having a contract that certain data format and type are going to expose help application developer and UI developer to work independently.
Here I am going to describe of creating RestFul webservice with NodeJS in 5 min.
Before we go forward, we need some bits.
Pre-Requestic:
This process can be perform in any platform. I will be using in Linux(Red Hat).
First get NodeJs binary and extract to a location.
If you want for other platform and OS head to NodeJsdownload Export the path to include the node binary location.
Export PATH=$PATH:node/bin
Create a project directory where you want to create the webservice.
INSTALL EXPRESS.
I am using older version of express and if you want to use latest version, some syntax might need to tweak as it is in development.
ACTUAL DEAL
Now create a file myservice.js
and copy the following code: or get from github
The type and content of response can be change according to your need. You can read from a file, propertyfile, db – (go crazy to get from any source)
The url and port can also be change to whatever your need.
Now save the file and startup your web server.
node myservice.js
To Test for GET, you can either use curl or browser to get the result.
http://localhost:1337/movie/getMovie/MrAwesome