1/1/1970
req & res objectsreq (request) and res (response) are objects automatically provided by express when handling HTTP requests.
req (Request Object): Contains details about the incoming request.
- req.query – Query parameters (/user?id=1)
- req.params – Route parameters (/user/:id)
- req.body – Request body (POST/PUT)
- req.headers – HTTP headers
res (Response Object): Used to send data back to the client.
- res.send() – Send text/HTML/JSON
- res.json() – Send JSON data
- res.sendFile() – Send a file
- res.status() – Set HTTP status code
Middleware in Express is a function that has access to the request (req) and response (res) objects, and the next function (next) in the application’s request-response cycle.
Middleware Functions
next() function.Order of Middleware
next(), the request-response cycle is considered complete, and no further middleware will be executed.Types of Middleware
app.use((req, res, next) => {
console.log('Request made!');
next(); // Pass control to the next middleware
});app.get('/about', (req, res, next) => {
console.log('Accessing About Page');
next(); // Pass control to the route handler
});app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something went wrong!');
});express.json(), express.urlencoded(), express.static(), express.Router()app.use(express.static('public')); // Serve static files from the 'public' foldermorgan for logging, cors for handling cross-origin requests).app.use() : Used to apply middleware functions that run on every request or specific routes.
app.use([path], middleware); // path is optinalpath is optional. specifies a path for the middleware. If omitted, it runs globally on all routes.GET, POST, PUT, etc.) unless specified otherwise.app.get() : Defines a route handler for GET requests at a specified URL path.
app.get(path, middleware, callback);path: The URL or route where the GET request is made.middleware is Optional middleware function(s) executed before the final handler.callback: The final function that handles the GET request.app.post() : Defines a route handler for POST requests at a specified URL path.
app.post(path, middleware, callback);app.put() : Defines a route handler for PUT requests at a specified URL path.
app.put(path, middleware, callback);app.delete() : Defines a route handler for DELETE requests at a specified URL path.
app.delete(path, middleware, callback);Note: Square brackets [] are used in documentation or examples to indicate optional parameters, but in this case, the middleware itself is required to be a function.
Conclusion:
app.use() is used to define global middleware that works for all routes and HTTP methods.app.get() and other methods like app.post() are used to define route-specific middleware for a particular route (e.g., /hello).// Middleware
const midd = (req, res, next) => { ... };// Route Handler (sends the response)
app.get('/', myMiddleware, (req, res) => {...}
);const midd = (req, res,next) => {}
next() Required to pass control to the next middleware.(req,res)=>{}
next() Not needed (It ends the request-response cycle).Note: In Express.js, the route handler is a callback function that processes a specific route.
static / public folder
static (or public) -> used to serve static assets/files to the clientImages, Stylesheets, JavaScript, Fonttemplate folder
template folder (or templates in Flask) -> used to store HTML template files that your web application renders.HTMLexpress() vs expressexpress() : creates an instance of an Express application.
express(), it returns an Express application object that can be used to define routes, middleware, and handle HTTP requests.app.get(), app.post(), app.put(), app.delete(), app.use(), app.listen(), app.route(), app.set(), app.get(), app.all(), app.param()const app = express(); // Creates an Express
app.use(...) // Use instance of `express()`express : express module
express library.express(), express.Router(), express.static(), express.json(), express.urlencoded(), express.raw(), express.text()const express = require('express'); // Import the Express module
express.static() // use 'static()' middleware of express module