s*****w 发帖数: 1527 | 1 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 ! |
p*****2 发帖数: 21240 | |
s*****w 发帖数: 1527 | 3 大牛请多展展,我是刚开始学node。
最土的方法据说是每个session开一个socket,然后node server根据socket handle来
区分。
但我想总有现成简单的方法吧。
谢谢!
【在 p*****2 的大作中提到】 : stateless吧?
|
l**********n 发帖数: 8443 | 4 一个session可以开n个socket.
甚至一个page可以开多个websocket.
不同的socket干不同的事情
【在 s*****w 的大作中提到】 : 大牛请多展展,我是刚开始学node。 : 最土的方法据说是每个session开一个socket,然后node server根据socket handle来 : 区分。 : 但我想总有现成简单的方法吧。 : 谢谢!
|
l**********n 发帖数: 8443 | |
s*****w 发帖数: 1527 | 6 大牛有没有更简单的办法?
【在 l**********n 的大作中提到】 : 一个session可以开n个socket. : 甚至一个page可以开多个websocket. : 不同的socket干不同的事情
|
s*****w 发帖数: 1527 | 7 不会做啊,就听别人说了
【在 l**********n 的大作中提到】 : 只是不明白你为什么要用socket?
|
l**********n 发帖数: 8443 | 8 筒单的吧?用query url.
【在 s*****w 的大作中提到】 : 大牛有没有更简单的办法?
|
s*****w 发帖数: 1527 | 9 我什么都不会,:(
能给我写一段吗?伪币答谢。。。
【在 l**********n 的大作中提到】 : 筒单的吧?用query url.
|
l**********n 发帖数: 8443 | 10 like this:
?page=page1
?page=page2
on server side:
switch(req.query.page){
case 'page1':break;
case 'page2':break;
default:break;
}
【在 s*****w 的大作中提到】 : 我什么都不会,:( : 能给我写一段吗?伪币答谢。。。
|
|
|
s*****w 发帖数: 1527 | 11 可能我没说清楚,
2个web browser 都可以同时打开page1的,你这个可以区分吗?
谢谢!
【在 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 发帖数: 8443 | 12 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.
【在 s*****w 的大作中提到】 : 可能我没说清楚, : 2个web browser 都可以同时打开page1的,你这个可以区分吗? : 谢谢!
|
s*****w 发帖数: 1527 | 13 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.
|
b********0 发帖数: 62 | 14 http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("");
}).listen(8080);
函数里不是这个req res成对的么 根据req给res返回就行了啊 不会混的啊
如果是要区分用户的话
response.writeHead(200, {
'Set-Cookie': 'key=value',
'Content-Type': 'text/plain'
});
不同的用户给个不同的value就行了 然后parse req就可以区分用户了 |
s*****w 发帖数: 1527 | 15 先容我理解一下,谢谢了!
【在 b********0 的大作中提到】 : http.createServer(function (req, res) { : res.writeHead(200, {'Content-Type': 'text/plain'}); : res.end(""); : }).listen(8080); : 函数里不是这个req res成对的么 根据req给res返回就行了啊 不会混的啊 : 如果是要区分用户的话 : response.writeHead(200, { : 'Set-Cookie': 'key=value', : 'Content-Type': 'text/plain' : });
|
d****n 发帖数: 1637 | 16 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 发帖数: 1527 | 17 非常谢谢,让我去学习一下。
【在 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 发帖数: 825 | |
s*****w 发帖数: 1527 | 19 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.
|