Previously we saw how to build a basic web server using http module of NodeJS. But there were some drawbacks regarding code maintainability. Hence a library/module for NodeJS was developed -- ExpressJS, which can handle the routes and functions of your server.
As we said, ExpressJS is a NodeJS module which is not a core NodeJS module. This means we need install express for our application.
Install ExpressJS by running “ npm install express --save ” in the terminal in your project directory. That is all we need to do.
Now we will create the same server we previously built using ExpressJS.
Change your server.js file to:
Here we require the express module and create a new instance of express which will be our server. With this server, we specify the handler for a url.
All the assigning of handlers to the routes should be called before app.listen(). The order in which also matters as the routes are matched from top to bottom. The handler that matches the route first is executed independent of the next handlers.
Restart your server and open your web browser and visit the previous urls. You should get the same responses as before.
As with the http module, the handler function contain req, res and a next (optional) arguments which you can use to achieve various functions. The req object stores information about the request like the url, params body etc while the res object stores info about the response.
First, a little more about the request object
Two commonly used support modules for ExpressJS are cookie-parser and body-parser. These were a part of ExpressJS 3.0 but were moved to independent modules in version 4.0. Cookie parser is used to parse the cookies stored in client and store them in req.cookies object while the body
Here we show the functioning of body-parser. Cookie-parser will be explained when we will create a full web app using angular.
Install these modules by doing
$ npm install body-parser --save
$ npm install cookie-parser --save
After installing we need to tell the express app to use these modules. Modify the server.js to include these modules with main app:
Till now we have used res.send() to send data to the client. If we need to send a JSON object as response, we can send using res.json() which will send it as a JSON instead of converting it into string.
Now for testing a post request, we will use an app called Postman. This chrome app is available for all the platforms and is widely used for the same.
Start the app and send a POST request to http://localhost:3000/user with the data of your choice.
An example is shown:
An example is shown:
Send this post request. You should get a response like
The body-parser module parses the POST request into a json object stored in req.body body object. You can then individually process the fields as per the app logic. In the above example we simply display the content of the request body as the response for the request.
This is just the surface of ExpressJS framework. There are lots of functionalities which we will see in due course of this tutorial.
Let's do another simple task. Store the POST request and send it when requested through GET “/user” end point.
This is the sample response to the GET /user endpoint after 2 POST requests.
Similarly you can use PUT, HEAD, DELETE, GET or OPTIONS request types. Each one of these have a different use as you saw in article about RestfulAPI.