Javascript 使用节点上的Express在主页上捕获cookie(req.headers.cookie)
我正在做一个基本的身份验证示例。我有Node、Express和Cookie。一旦用户登录,我就制作并存储cookie。刷新页面后,我想使用cookie显示用户仍然登录到响应中,并提供与该用户相关的信息 服务器端:Javascript 使用节点上的Express在主页上捕获cookie(req.headers.cookie),javascript,node.js,cookies,server-side,Javascript,Node.js,Cookies,Server Side,我正在做一个基本的身份验证示例。我有Node、Express和Cookie。一旦用户登录,我就制作并存储cookie。刷新页面后,我想使用cookie显示用户仍然登录到响应中,并提供与该用户相关的信息 服务器端: // If I put the app.get('/'.....) up here I get the response, but not the page HTML/JS/CSS/etc... // This uses the /app as the root directory
// If I put the app.get('/'.....) up here I get the response, but not the page HTML/JS/CSS/etc...
// This uses the /app as the root directory
app.use(express.static(__dirname + '/app'));
// Here I get the page HTML/JS/CSS/etc but can't capture the cookie
app.get('/', function(req, res) {
console.log('I want to get here');
if(req.headers.cookie){
// This parses the cookies into a usable array
var incoming_cookies = cookie.parse(req.headers.cookie);
Person.find({...})
.then( function(results) {
if (weDontFindSomeone) {
console.log('user not found');
res.status(400).send('user not found');
} else {
if (incoming_cookies.uname === loggedIn.uname) {
console.log('Starting with a cookie logged in');
res.status(200).send(results);
} else {
console.log('Some other problem with cookies');
res.status(400).send('Some other problem with cookies');
}
}
})
.catch(function(error){
res.status(400).send('some other error in starting, error: ' + error);
});
} else {
res.status(200).send('Starting from scratch.');
}
});
app.post('/api/login', function (req, res) {
console.log('Getting a login request');
if (weValidateTheCredentials) {
Person.find({...})
.then(function(personResults) {
if (personResults.rowCount === 0) {
res.status(400).send('user not found');
} else {
console.log('Logging in at \'/api/login\', and sending a cookie.');
// 3 hours max age
res.cookie('UID', req.body.uid, {maxAge: 10800000});
res.cookie('uname', req.body.uname, {maxAge: 10800000});
res.status(200).send(personResults);
}
})
.catch(function(error){
res.status(400).send('some other error in logging in, error: ' + error);
});
} else {
res.status(400).send('login requires a uname and pwhash');
}
});
如何将请求中的cookie捕获到主页,并使用该cookie确定向客户提供了什么
- 我把它放在客户端的JS中了吗
- 如果您想推荐另一个节点模块,请在plkr、fiddle、网页示例等中显示一个工作示例。我在学习工作代码方面做得更好,因为我花了很长时间才到达这个桥:)
// If I put the app.get('/'.....) up here I get the response, but not the page HTML/JS/CSS/etc...
// This uses the /app as the root directory
app.use(express.static(__dirname + '/app'));
// Here I get the page HTML/JS/CSS/etc but can't capture the cookie
app.get('/', function(req, res) {
console.log('I want to get here');
if(req.headers.cookie){
// This parses the cookies into a usable array
var incoming_cookies = cookie.parse(req.headers.cookie);
Person.find({...})
.then( function(results) {
if (weDontFindSomeone) {
console.log('user not found');
res.status(400).send('user not found');
} else {
if (incoming_cookies.uname === loggedIn.uname) {
console.log('Starting with a cookie logged in');
res.status(200).send(results);
} else {
console.log('Some other problem with cookies');
res.status(400).send('Some other problem with cookies');
}
}
})
.catch(function(error){
res.status(400).send('some other error in starting, error: ' + error);
});
} else {
res.status(200).send('Starting from scratch.');
}
});
app.post('/api/login', function (req, res) {
console.log('Getting a login request');
if (weValidateTheCredentials) {
Person.find({...})
.then(function(personResults) {
if (personResults.rowCount === 0) {
res.status(400).send('user not found');
} else {
console.log('Logging in at \'/api/login\', and sending a cookie.');
// 3 hours max age
res.cookie('UID', req.body.uid, {maxAge: 10800000});
res.cookie('uname', req.body.uname, {maxAge: 10800000});
res.status(200).send(personResults);
}
})
.catch(function(error){
res.status(400).send('some other error in logging in, error: ' + error);
});
} else {
res.status(400).send('login requires a uname and pwhash');
}
});
Cookie的方法有点迂回,你可以使用,它是专为express设计的 这很简单,主页上有一个例子:
var express = require('express')
var cookieParser = require('cookie-parser')
var app = express()
app.use(cookieParser())
app.get('/', function(req, res) {
console.log("Cookies: ", req.cookies)
})
app.listen(8080)
// curl command that sends an HTTP request with two cookies
// curl http://127.0.0.1:8080 --cookie "Cho=Kim;Greet=Hello"
或者使用您的代码:
var cookieParser = require('cookie-parser');
// Add cookie parser
app.use(cookieParser());
app.get('/', function(req, res) {
console.log('I want to get here');
if(req.cookies){
Person.find({...})
.then( function(results) {
if (weDontFindSomeone) {
console.log('user not found');
res.status(400).send('user not found');
} else {
if (req.cookies.uname === loggedIn.uname) {
console.log('Starting with a cookie logged in');
res.status(200).send(results);
} else {
console.log('Some other problem with cookies');
res.status(400).send('Some other problem with cookies');
}
}
})
.catch(function(error){
res.status(400).send('some other error in starting, error: ' + error);
});
} else {
res.status(200).send('Starting from scratch.');
}
});
// This uses the /app as the root directory
app.use(express.static(__dirname + '/app'));
我混合了服务器应该处理什么和客户端应该处理什么的范例
使用Jquery插件“Jquery cookie master”,我可以根据客户端的请求,使用
if($.cookie('attributeWanted')){…}
在何处以及如何设置cookie?@mscdex post检查cookie。谢谢你的关注!我从cookie解析器开始,但那不起作用。使用您提供的相同代码,它仍然不起作用。我只使用了一个简单的控制台,但它不起作用app.get('/',function(req,res){console.log('I finally got here');res.send();//或res.status(200.send('gh'))代码>仅当其位于根路径的快速定义之前时才起作用。我希望它使用cookies并将内容发送到适当的路径。一定很简单。页面正在请求带有cookies的路径“\”。我不确定curl
ing是正确的方法,但如果是,请解释。我甚至没有进入控制台日志。这就像定义根路径后路由失败一样。这对于其他路线来说没有意义,因为它们工作得很好。我真的看不出它有什么问题,你能发布你的整个文件吗?我想我有一个基本情况的cloud9实例在:(它不会自动转到/app,但一旦你点击它,它会带你到那里…)我不确定这是否足够,因为我只是为了这个学习cloud9。完整的更新代码可以在以下位置找到:特别是我从未使用过cloud9的server.js,我必须在单击链接时登录。没关系,我通过移动路由下面的静电来修复它。您可以通过/index.html和/route in/访问页面。如果你想在/上使用html,你必须将你的/路由重命名为其他我不喜欢的东西,你永远不应该信任客户端数据,它可以被修改,我不应该。我只是想在同一个请求上同时获取HTML和cookie。(您上一个答案,现在已删除)让我尝试使用两条路线,用于主页!?)。对于这样一个简单的设置来说,这个答案太复杂了,所以这让我发现我走错了路。我现在所做的是将代码与读取cookie的HTML一起提供。如果客户端更改了代码或cookie,则没有问题。我仍然需要对所有数据请求进行服务器端身份验证,以提供更多的HTML和JS。我想在提供“/”时读取一些cookie属性。