NodeJs Series Part 3: Let’s talk about Express and Restful
This is third article in series, I recommend you to go through previous two articles if you haven’t been through them though you can read it without having to know the previous articles.
Express
Express says, its Fast, unopinionated, minimalist web framework for node. so it basically handles all the server side stuff which includes
- Robust routing
- Focus on high performance
- Super-high test coverage
- HTTP helpers (redirection, caching, etc)
- View system supporting 14+ template engines
- Content negotiation
- Executable for generating applications quickly
Express in Action
Express is the world’s most simplistic server side implementation, with few lines of code your server is up and running, let’s see how we can implement a get route with few lines of code.
Boom, its just it, nothing fancy imports, no configuration, its simple as you like. req and res params are important, req contains all the data related to request, let me point out few
Request
- query params (if any)
- query string (if any)
- body data (if any)
- headers ( contains sensitive information, data type(i.e application/json) and security flags)
further explore this link to know more about request and associated methods.
Response
- response headers
- custom response i.e json data, html static pages, serve side rendered response
Get Muhammad Usman Ali Baloch’s stories in your inbox
Join Medium for free to get updates from this writer.
further check this link to explore response object
https://expressjs.com/en/4x/api.html#res
Let’s dig into some details
There are some advance but simple functionality associated with express, let me name them
- Middleware
- Validation
Middleware
Initially developers get overwhelmed by the concept of middleware but let me tell you it is simple and easy tool of express, let me explain what is middleware.
Middleware are basically simple javascript functions that can be executed before(all or specific) and after request(all or specific). That’s it, did you read that, its simple!
The middlewares that are executed before all requests are application level middleware but the one which are executed before specific request i.e /user, called route level middleware and the one that comes out of the box with express are built-in middleware.
further explore the middleware on this link
http://expressjs.com/en/guide/using-middleware.html#middleware.router
Middleware in action
Now you see, middleware is a simple js function and it is being passed an argument to same get method, what is the point of having an additional function, let me point out few
- Security ( check if user is authorized)
- pre-processing ( file upload, data validation etc)
A point to remember, we can have n numbers of middleware, in the example given below it has 2 middleware functions.
What the hell is this next?
Next is merely a function that initiates the next middleware in the chain and middleware follows the chronicle order(as they appear), next calls the upcoming middleware function in the chain, if there is nothing in the chain the execution stops.
Express validator
Express validator is basically use to validate the data i.e req.body ( you want to validate the data coming from request, i.e name is string with at least 6 in length, the password with 8 in length and must have one special character.
For further details visit this link
https://express-validator.github.io/docs/
That’s it about this story, hope you would like it and also provide me with feedback, I improve a lot on it. Looking forward to hear from you guys.





