Hello, learner! Today, we're going to learn about Basic Middleware and JSON Responses in Express.js
.
Our main objectives today are to:
Express.js
.Let's get started!
Middleware functions are like traffic controllers for requests and responses. They have access to both the incoming request object (req
) and the outgoing response object (res
).
Here's a simple definition:
JavaScript1function(req, res, next) { 2 // Middleware function code here 3}
Key Points:
req
: The request object.res
: The response object.next
: A callback to move to the next middleware function. Without calling next()
, the request will not proceed to the following middleware or route handler.For example, adding a requestTime
property to our request object can help with logging and debugging by tracking when a request was made. This can be useful for performance monitoring:
JavaScript1var myLogger = function (req, res, next) { 2 req.requestTime = Date.now(); 3 next(); 4}
This middleware adds the current date and time to every request received.
Now that we understand what middleware is, let's see how we can use it in our Express.js
application. First, we need to tell Express.js
to use the middleware function:
JavaScript1var express = require('express'); 2var app = express(); 3 4app.use(myLogger);
This setup runs the myLogger
middleware on every request. The middleware prepares requestTime
for all incoming requests, which can be useful for tasks like logging. Middleware is also widely used for error handling, authentication, and parsing incoming request bodies.
JSON is a simple format for structuring data that's easy for both humans and machines to read. It's commonly used in web applications to exchange data between a client and a server, for example, sending user profile information upon login.
Here’s what JSON looks like:
JSON1{ 2 "firstName": "John", 3 "lastName": "Doe", 4 "age": 25 5}
JSON responses are critical in modern web applications for tasks like fetching user information or submitting form data. Now that we've set up middleware, let's move on to handling data with JSON, a cornerstone of web communication.
JSON responses are common in Node.js
servers. Here's how you send JSON data in an Express
app:
JavaScript1app.get('/api', function (req, res) { 2 res.json({ firstName: "John", lastName: "Doe", age: 25 }); 3});
This route sends back a JSON object when accessed. Such responses are essential for enabling seamless communication between the server and client.
Middleware and JSON responses work well together. Middleware can modify request and response objects. For instance, prepare a JSON response in middleware and send it in a route:
JavaScript1// Middleware function to add property 2app.use(function (req, res, next) { 3 req.JSONResponse = { firstName: "John", lastName: "Doe", age: 25 }; 4 next(); 5}); 6 7// Route to send JSON response 8app.get('/api', function (req, res) { 9 res.json(req.JSONResponse); 10});
This middleware sets up the JSON response, and the route sends it when accessed.
Congratulations! You've learned about:
Express.js
Express.js
applicationNext, you'll have some practice exercises to apply what you've learned. This hands-on practice is key to mastering these concepts. Let's dive in and start practicing!