This is the flow, web client Node server backend program i could have 2 different browse tabs for the same URL, in the 1st one i click "send me info A from backend", in 2nd webpage i click "send me info B from backend". Now how do i distinguish A and B in the node.js ? Was looking for sample for session/cookie, just cannot find the right sample. Thanks very much !
【在 l**********n 的大作中提到】 : like this: : ?page=page1 : ?page=page2 : on server side: : switch(req.query.page){ : case 'page1':break; : case 'page2':break; : default:break; : }
l*n
17 楼
two different browsers could share the same session ID, or have different sessions. if you open two pages in the same browser, they will have the session ID. on the server side, they belong to two different sessions.
if i open 2 pages in the same browser, in 1st page i click "button A", in 2nd i click "button B". if there have the same session ID, how can i distinguish them ? (for our backend, there's only 1 pipe from web server to backend, when i get data back, i need to know where to send the result to ...)
【在 l**********n 的大作中提到】 : two different browsers could share the same session ID, or have different : sessions. if you open two pages in the same browser, they will have the : session ID. : on the server side, they belong to two different sessions.
generate uuid in cookie if not exists https://github.com/broofa/node-uuid uuid will be bound with cookie and in your request object. bind some information for each uuid then you will know what the page state is.
sample.
【在 s*****w 的大作中提到】 : This is the flow, : web client Node server backend program : i could have 2 different browse tabs for the same URL, : in the 1st one i click "send me info A from backend", : in 2nd webpage i click "send me info B from backend". : Now how do i distinguish A and B in the node.js ? : Was looking for sample for session/cookie, just cannot find the right sample. : Thanks very much !
s*w
22 楼
非常谢谢,让我去学习一下。
【在 d****n 的大作中提到】 : generate uuid in cookie if not exists : https://github.com/broofa/node-uuid : uuid will be bound with cookie and in your request object. : bind some information for each uuid then you will know what the page state : is. : : sample.
j*f
23 楼
这个用socketio应该很容易
s*w
24 楼
Can dryden and other big cow help me to complete this, Thanks very much ! var express = require("express"); var session = require("express-session"); var app = express(); //Creating Router() object var router = express.Router(); // Router middleware, mentioned it before defining routes. router.use(function(req,res,next) { console.log("/" + req.method); next(); }); router.use("/user/:id",function(req,res,next){ console.log(req.params.id) if(req.params.id == 0) { res.json({"message" : "You must pass ID other than 0"}); } else next(); }); // Provide all routes here, this is for Home page. router.get("/",function(req,res){ res.json({"message" : "Hello World"}); }); router.get("/user/:id",function(req,res){ res.json({"message" : "Hello "+req.params.id}); }); // Tell express to use this router with /api before. // You can put just '/' if you don't want any sub path before routes. app.use("/api",router); app.use( session( { genid: function(req) { return genuuid() }, secret: 'keyboard cat', resave: false, saveUninitialized: true } )); // Listen to this Port app.listen(8888,function(){ console.log("Live at Port 8888"); });
【在 d****n 的大作中提到】 : generate uuid in cookie if not exists : https://github.com/broofa/node-uuid : uuid will be bound with cookie and in your request object. : bind some information for each uuid then you will know what the page state : is. : : sample.