If you search this on google, you will see
What is a middleware in Express?
Middleware functions are functions that have access to the request object ( req ), the response object ( res ), and the next function in the application's request-response cycle.
But trust me it is more easy to learn, but this definition seems little complex.
When you create server you call this method
app.get("/",function(req,res){
res.send("Hello");
});
When you handle post requests you call method like this
app.post("/compose",function(req,res){
res.redirect("/");
});
When you work with any module like, body-parser, you write this
app.use(bodyParser.urlencoded({extended: true}));
These are all middlewares. YES
Middlewares run between the time server get request and server responds to a request.
Anything between request to server and response by server is middleware be it get or use or post method.
The syntax of middleware in express documentation is - This is application level middleware, we'll look at this type later in this blog
const express = require('express')
const app = express()
app.use((req, res, next) => {
console.log('Time:', Date.now())
next()
})
What is this next in function signature and what is this next() here?
This is the syntax of using middleware, we should add this in middleware function definition. next simply specifies the next middleware in sequence.
next() calls next middleware in sequence(program sequence)
One more thing
- app.method -> get/post/put/delete
- app.all -> handles all HTTP request
- app.use -> specify 'middleware' as callback function, we can use this to use any function(middleware) globally
Simplest middleware
app.get("/",function(req,res){
res.send("Example);
})
Types of middleware
Application level middleware -> app.use() / app.method()
Bind application-level middleware to an instance of the app object by using the app.use() and app.METHOD() functions, where METHOD is the HTTP method of the request that the middleware function handles (such as GET, PUT, or POST) in lowercase.
Router level middleware -> express.Router()
Router-level middleware works in the same way as application-level middleware, except it is bound to an instance of express.Router().
const router = express.Router()
Load router-level middleware by using the router.use() and router.METHOD() functions.
Error handling middleware
app.use(err,req,res,next)
Define error-handling middleware functions in the same way as other middleware functions, except with four arguments instead of three, specifically with the signature (err, req, res, next)):
Built-in middleware -> express.static, express.json, express.urlencoded
express.static serves static assets such as HTML files, images, and so on.
express.json parses incoming requests with JSON payloads. NOTE: Available with Express 4.16.0+
express.urlencoded parses incoming requests with URL-encoded payloads. NOTE: Available with Express 4.16.0+
3rd party middleware
These are installed with npm, and then we require these
Use third-party middleware to add functionality to Express apps.
Install the Node.js module for the required functionality, then load it in your app at the application level or at the router level.
The following example illustrates installing and loading the cookie-parsing middleware function cookie-parser.
$ npm install cookie-parser
const express = require('express')
const app = express()
const cookieParser = require('cookie-parser')
// load the cookie-parsing middleware
app.use(cookieParser())
Bonus
- Handler function
app.get("/contact", function(req,res)=>{
res.send("hello everyone");
)}
- Meaning of routing function definition
app.method(PATH, HANDLER)
app - instance of express
method - HTTP request method(get,post,put,delete)
PATH - Path on server
HANDLER - Function which executes when route is matched
Thanks for reading
React to this blog if you loved it
If you have any suggestions and corrections, do mention it in comments
Top comments (0)