Middleware : The Key To Express
- Middleware are just functions
- Each middleware has access to the request and response objects
- Middleware can end the HTTP request by sending back a response with methods like res.send()
- Middleware can be chained together, one after another by calling next()
The job of those middleware is to parse the incoming body of the request and add it to the request object. So that we can use it as a JavaScript object.
Express is a routing and middleware web framework that has minimal functionality of its own: An express application is essentially a series of middleware function calls
Middleware functions are functions that have access to the request object(req), response object(res) and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.
Middleware functions can perform the following tasks:
- Execute any code
- Make changes to the request object and the response object
- End the request-response cycle
- Call the next middleware function in the stack
If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.
An express application can use the following types of middleware:
- Application-level middleware
- Route-level middleware
- Error-handling middleware
- Build-in middleware
- Third-party middleware
In this blog, we will take a look into an external middleware
- It’s very easy to use
- We need to install Morgan if we want to use
npm install morganIn order to use this as application, you need to tell to your application to this Morgan as middleware
const morgan = require('morgan')
app.use(morgan('tiny'))app.use()
If i have any code/function inside of app.use()
Then it will become a function/code that will run on every single incoming request.
Get Avinash Dogiparthi’s stories in your inbox
Join Medium for free to get updates from this writer.
app.use() allows us to run code on every single request
Ex1:
app.use(express.static())This is telling express to use or to serve static files on every request
Ex2:
app.use(express.json())This is telling express to parse the body as JSON on every single request
Ex3:
app.use(express.urlencoded())express.urlencoded() is a method inbuilt in express to recognize the incoming Request Object as strings or arrays. This method is called as a middleware in your application using the code: app.use(express.urlencoded());
By using Morgan as middleware, we can able to see
- What kind of request?
- What kind of route?
- Status code
- Response time
I believe you can read more about what Morgan does, but the point here is that we have injected this little thing in the middle and we could do that ourself
Before wrapping up, let’s just take a quick look at Morgan
There are some pre-defined formats to use Morgan, you can see it in the docs of Morgan.
app.use(Morgan('dev'))next()
In order to use this third parameter we should use this sytax
app.use((req,res,next) => {
console.log("This is my first middleware");
next();
})Ex 2 :
Setting Up a 404 route
By adding this at the end of all routes will give this response to the get request of any route that is not present in express server.









