0% found this document useful (0 votes)
2 views

middleware in express js

The document outlines five types of middleware in an Express application: application-level, router-level, error-handling, built-in, and third-party middleware. It provides examples of how to implement each type, including the use of functions to handle requests and errors. Additionally, it highlights built-in middleware functions for serving static assets and parsing JSON and URL-encoded payloads.

Uploaded by

P.Padmini Rani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

middleware in express js

The document outlines five types of middleware in an Express application: application-level, router-level, error-handling, built-in, and third-party middleware. It provides examples of how to implement each type, including the use of functions to handle requests and errors. Additionally, it highlights built-in middleware functions for serving static assets and parsing JSON and URL-encoded payloads.

Uploaded by

P.Padmini Rani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

These are all 5 different middleware of an express application.

 Application-level middleware.

 Router-level middleware.

 Error-handling middleware.

 Built-in middleware.

 Third-party middleware.

application-level middleware

app.use( (req, res, next) => {


console.log('Time:', Date.now())
next()
})
Router-level middleware

Router-level middleware works in the same way as application-


level middleware, except it is bound to an instance of
express.Router()

const express = require('express')


const app = express()
const router = express.Router()
// a middleware function with no mount path. This code is
executed for every request to the router
router.use((req, res, next) => {
console.log('Time:', Date.now())
next()
})

Error-handling middleware

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):

app.use((err, req, res, next) => {


console.error(err.stack)
res.status(500).send('Something broke!')
})

Built-in middleware

Express has the following built-in middleware functions:

app.use(express.static()) // serves static assets such as HTML


files, images, and so on
app.use(express.json()) // parses incoming requests with JSON
payloads. NOTE: Available with Express 4.16.0+
app.use(express.urlencoded()) // parses incoming requests
with URL-encoded payloads. NOTE: Available with Express
4.16.0+

Third-party middleware

you can install the node.js module for the required functionality,
then load it in your app at the application level or at the router
level.
const express = require('express')
const app = express()
const cookieParser = require('cookie-parser')

// load the cookie-parsing middleware


app.use(cookieParser())

You might also like