从 expressjs 请求发送数据到表单

Sending data from expressjs request to form

我有一个登录系统,用户可以在其中输入他们的信息,当他们提交信息时,我会使用 express 验证信息,如果信息无效,我会发送一条错误消息。现在我只是将 res.send 用于错误消息,我将如何重定向回我的表单但出现错误消息。我不想使用 url 参数,因为那不安全。

所以我的理解是,你想要你的登录表单,显示错误消息,例如密码错误。

const login = (req, res) => {
    const user = new userModel(req.body.email, req.body.password);
    const found = db.findUser(user);
    if (found) {
        if (user.password == found.password) {
            res.status(200).send(true);
        } else {
            res.status(401).json({ msg: 'The password is incorrect'});
        }
    } else {
        res.status(404).send(false);
    }
};

那你可以用msg 属性 是提取的密码不对

fetch('/api/users/login', {
 method: 'POST'
})
.then(res => res.json())
.then(data => {
document.getElementById('...some div').innerHTML = `<div>${data.msg}</div>`
})