首页 > 解决方案 > 使用 POST 在 HTML 文件中注册

问题描述

我目前正在为学校做一个项目。我正在构建后端和前端应用程序。我有 POST 在后端工作,我已经用邮递员对其进行了测试,它发布并保存在 MongoDB 中。如果它在浏览器上,我将如何让帖子工作?我有一个包含名字、姓氏、电子邮件和密码的表格。如何在浏览器上发帖?是否可以在实时服务器上发布?

我尝试将它托管在 heroku 上,但没有成功。我尝试在 react 上托管,但它可以工作,但如果可能的话,我宁愿在 liveserver 上进行。

这是我用于注册的 html 文件。

<!DOCTYPE html>
<html lang="en">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Register</title>
    <style>
        html{
            background-color:hsl(60, 20%, 80%);



        }
        input[type=text],
        input[type=password] {
            width: 100%;
            padding: 15px;
            border: black 1.5px solid;
            background: #f1f1f1;
        }

        input[type=text]:focus,
        input[type=password]:focus {
            background-color: #ddd;
            outline: none;
        }


        button {
            background-color: #4CAF50;
            color: white;
            padding: 14px 20px;
            margin: 8px 0;
            border: none;
            cursor: pointer;
            width: 100%;
            opacity: 0.7;
        }

        button:hover {
            opacity: 1;
        }

        .cancelbtn {
            padding: 14px 20px;
            background-color: #f44336;
        }

        .cancelbtn,
        .signupbtn {
            float: left;
            width: 50%;

        }



        .clearfix::after {
            content: "";
            clear: both;
            display: table;
        }
    </style>
</head>

<body>
    <form>
        <h1>Sign Up</h1>
        <p>Please fill in this form to create your accout.</p>
        <label for="firstname"><b>First Name</b></label>
        <input type="text" placeholder="Enter First Name" name="firstname" required>

        <label for="lastname"><b>Last Name</b></label>
        <input type="text" placeholder="Enter Last Name" name="lastname" required>

        <label for="email"><b>Email</b></label>
        <input type="text" placeholder="Enter Email" name="email" required>

        <label for="psw"><b>Password</b></label>
        <input type="password" placeholder="Enter Password" name="psw" required>

        <div class="clearfix">
            <button type="button" class="cancelbtn">Cancel</button>
            <button type="submit" class="signupbtn">Sign Up</button>
        </div>
    </form>


</body>

</html>

这是我的后端发布方法。

router.post('/', sanitizeBody, async (req, res) => {
      console.log(req.sanitizeBody);
      let password = await bcrypt.hash(req.body.password, saltrounds)
    new User({
        firstName: req.body.firstName,
        lastName: req.body.lastName,
        password,
        email: req.body.email,
        isStaff: req.body.isStaff
    }).save().then(result => {
        res.status(201).send(result);
    }).catch(err => {
        res.send(err);
    })

  })

我希望在 html 页面上发布,而不是在邮递员上发布。当我点击提交时,我希望它发布在 index.html 实时服务器上。

标签: javascripthtmlmongodbpostpostman

解决方案


推荐阅读