2018-10-12 15:37:13 -04:00
---
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 -->
2019-07-02 07:05:15 +05:30
Similar to the last challenge, but now we are chaining 2 functions together. It seems complicated, but it's just JavaScript.
2018-10-12 15:37:13 -04:00
2019-07-02 07:05:15 +05:30
### 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:
2018-10-12 15:37:13 -04:00
```javascript
2019-07-02 07:05:15 +05:30
app.get("/now", (req, res, next) => {
// adding a new property to req object
// in the middleware function
2018-10-12 15:37:13 -04:00
req.string = "example";
next();
2019-07-02 07:05:15 +05:30
}, (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
2018-10-12 15:37:13 -04:00
});
2019-07-02 07:05:15 +05:30
});
2018-10-12 15:37:13 -04:00
```
2019-07-02 07:05:15 +05:30
You can also declare the middleware beforehand to use in multiple routes as shown below:
2018-10-12 15:37:13 -04:00
2019-07-02 07:05:15 +05:30
```javascript
const middleware = (req, res, next) => {
req.time = new Date().toString();
next();
};
2018-10-12 15:37:13 -04:00
2019-07-02 07:05:15 +05:30
app.get("/now", middleware, (req, res) => {
res.send({
time: req.time
});
});
```