我想使用NodeJs和React创建一个登录系统。我想在Cookie中保存有关用户的信息,例如电子邮件,姓名。我使用cookie解析器,无法正常工作。当我尝试'res.cookie('name','user.name')'时没有任何反应
这是我的代码。我有一个名为 server.js 的文件:
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var MongoClient = require('mongodb').MongoClient
var cors = require('cors')
var app = express();
const user = require('./controller/user.js')
app.use(express.static('views'));
app.use(cookieParser());
app.use(cors())//asta e pt ca UI-ul e pe alt domeniu fata de backend
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.post('/login',user.getLogin)
app.listen(4000, function(){
console.log("Listening to port 4000")
})
和文件 user.js :
var MongoClient = require('mongodb').MongoClient
function getLogin(req, res){
let email = req.body.email
let password = req.body.password
db.collection('user').findOne({email: email, password: password})
.then((user)=>{
if(user){
res.cookie("user_email",user.email)
res.cookie("user_passwordd",user.password)
return res.json({
status: 'Users Log in',
email: email,
password: password
})
}
else{
return res.json({
status: 'Email or password is wrong',
email: '',
password: ''
})
}
})
}
能帮我一下吗?