【问题标题】:How can i modify html element in node js如何修改节点js中的html元素
【发布时间】:2022-01-26 15:59:05
【问题描述】:

所以我有一个网站我需要修改 HTML 元素在一个登录表单中,例如它会显示用户名是否错误我需要它检查服务器端然后修改它,以便它将文本显示给客户端-侧面

我的网站是聊天应用程序,我尝试的当前代码是

app.post("/signup", function (req, res) { 
function makerror(message) {
    // idk
}
if (req.body.username) {
    const emailregex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
    if (!req.body.email.match(emailregex)) {
        return makerror("Please enter a vaild Email");
    }
    const passwordregex = /^.{8,}$/
    if (!req.body.password.match(passwordregex)) {
        return makerror('Your password must be 8 characters long.')
    }
    const usernameregex = /^[a-zA-z0-9]{3,25}$/g
    if (!req.body.username.match(usernameregex)) {
        return makerror('Your username must contain only character from A to Z, Underscores and number from 0 to 9')
    }
    const data = new Userdata({ username: req.body.username, password: scureJs.encrypt(req.body.password), email: req.body.email })
    data.save()
    const obj = {
        username: req.body.username,
        password: req.body.password,
        email: req.body.email,
        unix: Math.round(new Date() * 1000)
    }
    const cookie = scureJs.serialize(obj)
    res.cookie('sessionId', cookie, {
        expires: false,
        httpOnly: true,
        sameSite: 'lax',
    })
    console.log(cookie)
    console.log(req.body)
}

})

我的应用是聊天应用

【问题讨论】:

    标签: html node.js


    【解决方案1】:

    如果您使用的是 nodejs,那么您需要先制作一个表单。通常我们把用户名和密码放在那里。 然后附加 sql 或数据库 felicities 来存储数据并在表单提交上检查这些。 然后在异步加载时显示您想要的输入。

    Client(Form Data)---->Server(NodeJS)---->Database Check
          ^                    |
          |                    |
          |                    |
           --------------------
    

    【讨论】:

    • 我做了一个表格..
    • @sleepy 老实说,我们需要更多上下文,例如如何接收请求?它是 API/Web 服务器吗?它是基于客户端的应用程序吗?等等,否则我们不能给出建议,除非他们就可能的方式提出建议。本质上你的问题太模糊了。
    • @MysticSeagull 我现在编辑了它
    • @sleepy 所以在这种情况下,您需要将错误返回到客户端,然后使用 javascript 显示收到的错误。基本上,直到没有返回错误才会成功,否则继续告诉用户存在验证错误。
    • 是的,我需要那个
    猜你喜欢
    • 2019-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多