node.js - nodejs - express how it is possible to make the global variables on all routes? -


how possible make lang global on routes ? ex:

app.post('/set/lang',function(req,res){    req.lang = 'en';    res.json({lang:'en'}); });  app.get('/get/lang',function(){ res.json({lang:req.lang}); }); 

but return // {lang:undefined} ??

how can this? > /get/lang => return {lang:en}

req.lang undefined becasue request /get/lang stateless , not know state of request done on /set/lang.

what need here session service such https://github.com/expressjs/session

you may want read more here http://expressjs-book.com/forums/topic/express-js-sessions-a-detailed-tutorial/

my understanding being want save user language foreach user , let them set via sort of language selector in client app.

note also, question confusing may refer global named parameters in expressjs implement https://github.com/expressjs/express-params.

or, may refer input content validation, see more @ http://expressjs.com/4x/api.html#app.param

finally version not provided assume latest 4.x branch.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -