Node.js-Express JS Middleware Tutorial for Beginners with Hindi Translation

Posted by






Nodejs-Express JS Middleware Tutorial for Beginners

Nodejs-Express JS Middleware Tutorial for Beginners

Express JS is a widely used web application framework for Node.js. It provides a robust set of features for building web applications and APIs. One of the key features of Express JS is its middleware system, which allows you to add custom functionality to your application’s request handling pipeline. In this tutorial, we will learn about Express JS middleware and how to use it in our Node.js applications.

What is Middleware?

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. These functions can perform tasks such as modifying the request and response objects, performing authentication, logging, etc. Middleware functions can be used to execute code before handling a request, after handling a request, or both.

Using Middleware in Express JS

In Express JS, middleware can be added to the application using the app.use() method. This method takes a middleware function as an argument and adds it to the middleware stack. Middleware functions can also be added to specific routes using the app.use() method with the route as the first argument.

Creating Custom Middleware

Custom middleware functions can be created by defining a function that takes the req, res, and next parameters and performs some custom logic. For example, the following middleware function logs the request method and URL:


function logRequest(req, res, next) {
  console.log('Received a ' + req.method + ' request for ' + req.url);
  next();
}
app.use(logRequest);

Express JS Middleware Tutorial in Hindi

एक्सप्रेस जेएस (Express JS) बहुत अधिक प्रयोग किया जाने वाला वेब एप्लिकेशन फ्रेमवर्क है जो नोड.जेएस (Node.js) के लिए है। यह वेब एप्लिकेशन और एपीआई बनाने के लिए मजबूत सुविधाओं सेट प्रदान करता है। एक्सप्रेस जेएस का एक मुख्य विशेषता इसकी मिडलवेयर सिस्टम है, जिसका उपयोग अपने एप्लिकेशन के रिक्वेस्ट हैंडलिंग पाइपलाइन में कस्टम फंक्शनालिटी जोड़ने के लिए किया जा सकता है। इस ट्यूटोरियल में, हम एक्सप्रेस जेएस मिडलवेयर के बारे में सीखेंगे और अपने नोड.जेएस एप्लिकेशन में इसका उपयोग कैसे करते हैं।