* Expanded the solution for the 'Get Route Parameter Input from the Client' challenge * Expanded the guide for the 'Use body-parser to Parse POST Requests' challenge * Rewritten guide for the 'Serve JSON on a Specific Route' challenge and fixed source link * Expanded the guide for the 'Serve Static Assets' challenge * Expanded solution to the 'Get Query Parameter Input from the Client' challenge and fixed links to source file * Added solution to the 'Chain Middleware to Create a Time Server' challenge and fixed link to source file * Rewrite the 'Start a Working Express Server' challenge * Expanded the guide for 'Expand Your Project with External Packages from npm' * Added reference to semantic versioning in 'Add a Version to Your package.json' * fix/remove-links+fix-solutions * fix/remove-more-links
54 lines
1.3 KiB
Markdown
54 lines
1.3 KiB
Markdown
---
|
|
title: Chain Middleware to Create a Time Server
|
|
---
|
|
## Chain Middleware to Create a Time Server
|
|
|
|
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
|
|
|
Similar to the last challenge, but now we are chaining 2 functions together. It seems complicated, but it's just JavaScript.
|
|
|
|
### Hint
|
|
|
|
Instead of responding with the time we can also add any arbitrary property to the request object and pass it to the next function by calling the `next()` method. This is trivial, but it makes for a decent example. The code will looks like this:
|
|
|
|
```javascript
|
|
app.get("/now", (req, res, next) => {
|
|
// adding a new property to req object
|
|
// in the middleware function
|
|
req.string = "example";
|
|
next();
|
|
}, (req, res) => {
|
|
// accessing the newly added property
|
|
// in the main function
|
|
res.send(req.string);
|
|
});
|
|
```
|
|
|
|
### Solution
|
|
|
|
```javascript
|
|
app.get("/now", (req, res, next) => {
|
|
req.time = new Date().toString();
|
|
next();
|
|
}, (req, res) => {
|
|
res.send({
|
|
time: req.time
|
|
});
|
|
});
|
|
```
|
|
|
|
You can also declare the middleware beforehand to use in multiple routes as shown below:
|
|
|
|
```javascript
|
|
const middleware = (req, res, next) => {
|
|
req.time = new Date().toString();
|
|
next();
|
|
};
|
|
|
|
app.get("/now", middleware, (req, res) => {
|
|
res.send({
|
|
time: req.time
|
|
});
|
|
});
|
|
```
|