Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

express pass data between middleware

function middleware1(req, res, next) {
  // Set data
  req.dataFromMiddleware1 = "Data of Middleware 1";
  // Go to next middleware
  next();
}
  
function middleware2(req, res, next) {
  console.log("We are in Middleware 2.");
  // Get Data of Middleware1
  console.log(req.dataFromMiddleware1);
  // Go to next middleware
  next();
}
  
// Handling Get Request '/'
app.get("/", middleware1, middleware2, (req, res) => {
  return res.send(req.dataFromMiddleware1);
});
 
PREVIOUS NEXT
Tagged: #express #pass #data #middleware
ADD COMMENT
Topic
Name
9+9 =