0% found this document useful (0 votes)
9 views2 pages

Routing

This document provides an overview of Node.js Express routing and middleware, detailing how to define routes based on HTTP methods and organize them using Express Router. It highlights the functionality of middleware, including logging, authentication, body parsing, and error handling, along with examples of request and response methods. Additionally, it lists various application methods for handling requests and responses in an Express application.

Uploaded by

mydo.maro.mane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Routing

This document provides an overview of Node.js Express routing and middleware, detailing how to define routes based on HTTP methods and organize them using Express Router. It highlights the functionality of middleware, including logging, authentication, body parsing, and error handling, along with examples of request and response methods. Additionally, it lists various application methods for handling requests and responses in an Express application.

Uploaded by

mydo.maro.mane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Node JS Express Note

Routing
• Define routes based on HTTP methods and URLs.
• Express Router helps organize routes and middleware
Middleware
• Functions that have access to the request object (req), the response object (res), and the next
middleware in the application’s request-response cycle
• Can execute code, modify request and response objects, end the request-response cycle, or call the
next middleware.
middleware is widely used to handle common tasks in web applications.

Here are some typical use cases:


Logging : Recording details about each request for debugging or auditing.
Authentication : Verifying user credentials before allowing access to certain routes.
Body Parsing : Parsing incoming request bodies so they can be easily accesse (e.g., parsing JSON data).
Error Handling : Catching and managing errors to send appropriate responses to the client.

The ' next ( ) ' function is a function in the Express router which, when invoked, executes the middleware
succeeding the current middleware

const myMiddleware = (req, res, next) => {


console.log('Middleware executed!') ;
next ( ) ; // Passes control to the next middleware function
};
// Apply the middleware to all routes
app.use(myMiddleware) ;

app.get( ' / ' , (req, res) => {


res.send('Hello, Middleware!')
}
Node JS Express Note
Application Method
Method Name Description
app.get(path, callback) Defines a route that handles GET requests.
app.post(path, callback) Defines a route that handles POST requests.
app.put(path, callback) Defines a route that handles PUT requests.
app.delete(path, callback) Defines a route that handles DELETE requests
app.use(path, middleware) Adds middleware to the application.
app.listen(port, callback) Starts the server and listens on the specified port.

Method Request

Method Name Description


req.query Contains the URL query parameters (e.g., req.query.name for /search?name=value)
req.body Contains data sent in the request body (useful for POST/PUT requests; requires body-parser-middleware).
req.method The HTTP method used (e.g., GET, POST).
req.url The full URL of the request.
req.path The path part of the URL
req.headers Contains the request headers.
req.cookies Contains cookies sent by the client (requires cookie-parser middleware)
req.ip The IP address of the client.
req.protocol The protocol used (e.g., HTTP, HTTPS)
req.secure A boolean that indicates if the request was made via HTTPS.
req.get(headerName) Returns the specified HTTP request header.
req.params Contains route parameters (e.g., req.params.id for /user/:id).

Method Response

Method Name Description


res.send ( ) Sends a response of various types (text, HTML, JSON, etc.)
res.json ( ) Sends a JSON response
res.status ( ) Sets the HTTP status for the response (e.g., res.status(404))
res.set(headerName , value) Sets a response header
res.cookie ( ) Sets a cookie to be sent to the client
res.clearCookie ( ) Clears a cookie
res.redirect ( ) Redirects the client to a different URL
res.render ( ) Renders a view template (e.g., using Pug, EJS)
res.sendFile ( ) Sends a file as a response
res.end ( ) Ends the response process
res.type ( ) Sets the Content-Type of the response

You might also like