node.js - Node express,how to transmit a request to another specific route? -


i want pre-hand on requests,and according params determinate using route. example.

app.js

app.use('/api/v1/user',user1); app.use('/api/v2/user',user2); 

user1.js

router.get('/',function(req,res,next){    var userid = req.query.userid;    res.json('this v1 response'+userid); }); 

user2.js

router.get('/',function(req,res,next){     var userid = req.query.userid;     res.json('this v2 response' + userid); });  router.get('/userlist', function(req,res,next) {     res.json('return user list v2 api');     }); 

when http://hostname/api/v1/user?userid=1

or http://hostname/api/v2/user?userid=2

or http://hostname/api/v2/userlist can correct response;

now want make request simple.

i want request http://hostname?api_version=1&userid=1&userlist=0 response. add prehand route this

app.js

app.use(prehand); app.use('/api/v1/user',user1); app.use('/api/v2/user',user2); 

prehand.js

app.use(function(req,res,next){   var version = req.query.api_version;   if(version==1) {      req.originalurl = '/api/v1/user';    }    if(version==2) {      req.originalurl = '/api/v2/user';    }    if(req.query.userlist == 1) {      req.orininalurl = '/api/v2/user/userlist';    }     next(); }); 

then,it's failed,the console report 404 not found error.so,is there way transmit request specific route?


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -