list of middlewares in express
In Express.js, middleware functions are functions that have access to the request object (req
), the response object (res
), and the next middleware function in the application's request-response cycle. They are essential for performing tasks such as parsing incoming requests, logging, handling sessions, authentication, etc.
Here are some common middleware in Express.js:
-
express.json()
: Parses incoming requests with JSON payloads. This middleware is responsible for parsing thereq.body
object. -
express.urlencoded()
: Parses incoming requests with URL-encoded payloads. This middleware is used for parsing form data from<form>
submissions. -
express.static()
: Serves static files and assets such as images, CSS files, and JavaScript files. -
morgan
: HTTP request logger middleware for logging request details. -
helmet
: Helps secure Express apps by setting various HTTP headers. -
cors
: Middleware for enabling Cross-Origin Resource Sharing (CORS) in Express apps. -
Session middleware:
express-session
: Adds session support to store data between HTTP requests.
-
Authentication middleware:
- Examples include Passport.js middleware (
passport.initialize()
andpassport.session()
) for authentication strategies.
- Examples include Passport.js middleware (
-
Error-handling middleware:
- Custom middleware functions to handle errors in Express applications (
app.use(function(err, req, res, next) {...})
).
- Custom middleware functions to handle errors in Express applications (
-
Compression middleware:
compression
: Compresses HTTP responses to reduce the size of the response body.
-
Body parsing middleware:
- Apart from
express.json()
andexpress.urlencoded()
, there are others likebusboy-body-parser
for handling multipart form data.
- Apart from