node.js - Express middleware - router.use issue -


i have simple code:

var express = require('express'); var router = express.router();  router.get('/', function (req, res, next) {       req.lectalapidata = {         model: email,         conditions: req.query     };      router.use(function(req,res,next){    //this not executing         console.log('do that')          res.json({ok:'ok'});     });  }); 

i doing wrong, according docs, says can use syntax: http://expressjs.com/guide/routing.html

what doing wrong?

perhaps it's because router.use nested inside router.get -

so question becomes - how create more middleware same route inside router.get middleware?

just keep adding functions router.get('/',, executed in order. don't forget call next.

router.get('/', function (req, res, next) {     req.lectalapidata = {         model: email,         conditions: req.query     };     next(); // pass off next middleware }, function(req,res,next){     console.log('do that')      res.json({ok:'ok'}); }); 

or better:

function dothis(req, res, next) {     req.lectalapidata = {         model: email,         conditions: req.query     };     next(); // pass off next middleware }  function dothat(req, res) {     console.log('do that')      res.json({ok:'ok'}); }  router.get('/', dothis, dothat); 

Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -