如何从客户端向服务器发送cookie?

时间:2015-12-22 10:03:22

标签: javascript angularjs node.js mongodb express

我正在使用MEAN Stack。我做了登录/注册和个人资料页面。我在cookie中保存用户ID,现在我想在服务器中发送我的id。

我从像这样的饼干中获取我的身份:

userId = $cookies.get('userId')

如何将此信息发送到服务器?我必须在DB中找到这样的信息,其ID如下:

var userId = "receivedId";
User.UserModel.findOne({ _id: userId }, function(err, data) {
    var User = {_id: data._id, user: data.user, date: data.date};
res.send(User); //I know how to send from server to client.
});

1 个答案:

答案 0 :(得分:0)

请检查您的Web服务器文档。如果您正在使用express,则必须首先配置cookie-parser中间件。之后,您将能够使用req.cookies访问客户端cookie。 - Leonid Beschastny

答案作者是:Leonid Beschastny

谢谢你。它有效!