javascript - 使用 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 实时服务器上。
解决方案
推荐阅读
- json - 使用jq根据key值合并两个json值
- json - jq比较两个json并替换值
- laravel-backpack - 尝试在列表视图中向 crud 操作添加通知
- intel - Intel Xeon CPU 如何处理竞争 AVX 单元的内核?
- javascript - javascript原型和继承
- angular - 如何对服务构造函数中调用的函数进行单元测试?
- sql - 如果同一组中存在另一条记录,则省略组中的记录
- python - 为什么 Python BeautifulSoup 返回一个空列表?
- python - Python 激活同名窗口
- python - ImportError:无法从“blog.models”导入名称“Blog”