Javascript 使用express.js和passport.js从POST获取表单数据

Javascript 使用express.js和passport.js从POST获取表单数据,javascript,node.js,express,passport.js,Javascript,Node.js,Express,Passport.js,所以我建了一个聊天室,一切都很好,直到我把系统改成只使用uid,下面是它的工作原理 当用户注册时,会为其分配一个uid,该uid与他的凭据一起放入数据库,因此每次用户登录时,都会要求服务器获取与您在文本框中键入的用户名相关联的uid,并将其设置为如下cookie app.post('/apolloLogin', passport.authenticate('local-login', { successRedirect: '/apolloMainchat', // redirect to

所以我建了一个聊天室,一切都很好,直到我把系统改成只使用uid,下面是它的工作原理

当用户注册时,会为其分配一个uid,该uid与他的凭据一起放入数据库,因此每次用户登录时,都会要求服务器获取与您在文本框中键入的用户名相关联的uid,并将其设置为如下cookie

app.post('/apolloLogin', passport.authenticate('local-login', {
    successRedirect: '/apolloMainchat', // redirect to the secure profile section
    failureRedirect: '/apolloLogin', // redirect back to the signup page if there is an error
    failureFlash: true // allow flash messages
},function(req, res){
   var username = res.query.username

    console.log("tettestset")
    //res.cookie('cookieName','randomUid', { maxAge: 900000, httpOnly: true });
}),
提交时调用setCookies

function setCookies() {
    var username = document.getElementById("login-username").value
    console.log(username)
    getLoggedInUserInfo(username)
}
function getLoggedInUserInfo(username){     
console.log(username)
socket.emit('getLoggedInUserInfo',username)
}
socket.on('loggedInUserInfo',function(uid){
    document.cookie = "uid=" + uid;
})
但这在注册页面上是个问题,因为用户在数据库中还没有关于他的任何信息

所以我在想,如果我可以使用express在服务器端设置cookie,但是我不能像这样使用express从表单中获取任何值,该怎么办

app.post('/apolloLogin', passport.authenticate('local-login', {
    successRedirect: '/apolloMainchat', // redirect to the secure profile section
    failureRedirect: '/apolloLogin', // redirect back to the signup page if there is an error
    failureFlash: true // allow flash messages
},function(req, res){
   var username = res.query.username

    console.log("tettestset")
    //res.cookie('cookieName','randomUid', { maxAge: 900000, httpOnly: true });
}),
在这里,我将获得与用户名关联的uid 然后像这样设置uid cookie

app.post('/apolloLogin', passport.authenticate('local-login', {
    successRedirect: '/apolloMainchat', // redirect to the secure profile section
    failureRedirect: '/apolloLogin', // redirect back to the signup page if there is an error
    failureFlash: true // allow flash messages
},function(req, res){
   var username = res.query.username

    console.log("tettestset")
    //res.cookie('cookieName','randomUid', { maxAge: 900000, httpOnly: true });
}),
res.cookie'cookieName','randomUid',{maxAge:900000,httpOnly:true}

但是用户名值总是为null,它说res.cookie不是一个函数

我在这里用express做错了什么

编辑

现在我正在尝试这个

app.post('/apolloSignup', passport.authenticate('local-signup', {
    successRedirect: '/apolloMainchat', // redirect to the secure profile section
    failureRedirect: '/apolloSignup', // redirect back to the signup page if there is an error
    failureFlash: true // allow flash messages
},function(req, res){
    console.log(req.body.username)
}));
第二次编辑

这就是我现在使用的,我从console.log中得到null

这就是我的HTML看起来的样子


看起来您正在引用res.query.username。这是无效的-你是说req.query.username吗?查询参数将位于请求对象上,而不是响应对象上


此外,如果您从HTML表单获取POST数据,那么这些值应该可以通过req.body而不是req.query访问。

我非常确定,一旦您到达failureRedirect/successRedirect,请求/响应周期就结束了,因此您不会使用下一个中间件。但是,如果您正确设置了passport本地身份验证,您应该可以访问req.user,其中将包含登录用户的用户名和其他信息


尝试在/apolloMainchat route中进行cookie设置,参考req.user。希望有帮助

用户名是HTML表单中的值吗?如果是这样,它将在req.body值中可访问,而不是在req.query中。req.query公开URL查询参数的值。实际上,看起来您引用的是res.query.username。这是无效的-您是指req.query.username吗?我尝试过使用console.logreq.body.username,但没有成功。您可以编辑您的答案以包含HTML表单的外观吗?