[英]Client side send value to node.js and set as cookie
I'm using 我正在使用
app.use(require('./middleware/im')({
maxAge: 30 * 1000,
reapInterval: 20 * 1000,
authentication: require('./libs/authentication/' + AUTH_LIBRARY)
}));
to authenticate the user(cookie based) in my node.js/express.js. 在我的node.js / express.js中验证用户(基于cookie)。 Now my problem is I need my client side(a PHP + javascript) server send a cookie value to the node server via GET(jsonp) so I can authenticate the user.
现在我的问题是我需要我的客户端(PHP + javascript)服务器通过GET(jsonp)将cookie值发送到节点服务器,以便对用户进行身份验证。 But If I do:
但是如果我这样做:
app.get('/cookie', function(){//set cookie here});
it won't work because the auth code in the middleware fires before they even call the action. 它不起作用,因为中间件中的auth代码甚至在调用操作之前就已触发。 What should I do so I can send a cookie(or any string) to the node.js server before the middleware fires?
我应该怎么做才能在中间件启动之前将cookie(或任何字符串)发送到node.js服务器? If that's not possible, is there any alternative way I can use so I can let the node.js server get the user's userid?
如果那是不可能的,那么我是否可以使用其他方法来让node.js服务器获取用户的用户ID?
Thank you. 谢谢。
To intercept the request, before your '.middleware/im'
can get to it, you need to create your own middlware, which is surprisingly simple (the following goes before your call to app.use(require('./middleware/im')({ ...
): 要拦截请求,您的
'.middleware/im'
才能到达它,您需要创建自己的Middlware,这非常简单(在调用app.use(require('./middleware/im')({ ...
):
app.use(cookieDetect);
function cookieDetect (request, response, next) {
if (request.url === "/cookie") {
// do your stuff here
} else {
next(); // keep the middleware chain going
}
}
Btw, as a JavaScript syntactical note, when you use function declarations (different than var myFunc = function () { ...
), "scope hoisting" happens: that particular function declaration is available at the very beginning of that scope-level's execution, instead of from where it appears in the code. 顺便说一句,作为JavaScript的语法注释,当您使用函数声明(不同于
var myFunc = function () { ...
)时,会发生“作用域提升”:该特定函数声明在该作用域级别的执行开始时可用,而不是代码中显示的位置。 Which is why I'm able to app.use
it before it appears in the code. 这就是为什么我能够在代码中出现它之前对其进行
app.use
原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.