freeCodeCamp/guide/english/certifications/apis-and-microservices/basic-node-and-express/chain-middleware-to-create-.../index.md

1013 B

title
Chain Middleware to Create a Time Server

Chain Middleware to Create a Time Server

Similar to the last challenge, but now we are chaining 2 functions together. It seems complicated, but it's just javascript.

Instead of responding with the time we can also add a string to the request and pass it to the next function. This is trivial, but it makes for a decent example. The code looks like:

app.get("/now", middleware(req, res, next) {
  req.string = "example";
  next();
},
  function (req, res) {
      res.send(req.string); // This will display "example" to the user
  });

Help our community expand these hints and guides.