Sqlite3 NodeJs 在登录时重定向



>我有一个要求,如果登录成功,用户应该重定向到欢迎页面,而用户应该在登录失败时收到错误消息。

我可以在控制台中打印数据,但我只需要一些帮助将用户重定向到有关成功和失败的网页。

以下是我可以在控制台上打印的应用程序.js功能

app.post('/loginform', function (req, res, next) {
let sql = `SELECT * FROM Users WHERE Username  = ? and Password = ?`;
let Username = `${req.body.name}`;
let Password = `${req.body.password}`;
let success;
db.get(sql, [Username, Password], (err, row) => {
if (err) {
return console.error(err.message);
}
return row
? console.log(`Login Success `+ row.ID, row.Username,row.Password)
: console.log(`Sorry, your username and password are incorrect. Try again!`);
});
});

我尝试打印返回为行的内容,并根据这些值,我了解到它在登录失败时返回未定义,并在登录成功时返回该用户行的 json 数据。

所以我想出了以下解决方案。

db.get(sql, [Username, Password], (err, row) => {
if (err) {
return console.error(err.message);
}
console.log(row);
if(row != undefined){
res.redirect('/welcome.html');
}
else{
res.write('<html><body>');
res.write('<h1>Sorry your username and password are incorrect. Try again!</h1>');
res.write('</body></html>');
res.send();
}
});

最新更新