我在服务器上发送数据时遇到问题。我在节点中运行server.js但是总是在点击提交按钮后req.body.username和req.body.password未完成... HTML文件在浏览器中打开,所以可能是问题所在,但我不知道如何解决它。我是初学者所以要理解......
的index.html:
<form method="post" action="http://127.0.0.1:3000/">
<fieldset>
<label for="username"></label>
<input type="text" id="username" placeholder="username">
<label for="password"></label>
<input type="password" id="password" placeholder="password">
<input type="submit" value="Create user">
</fieldset>
</form>
server.js:
const express = require('express');
const mysql = require('mysql');
const bodyParser = require('body-parser');
const http = require('http');
const template = require('./template');
const app = express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static('public'))
app.use(bodyParser());
app.post('/',(req, res) => {
template.createUser(
req.body.username,
req.body.password
)
});
app.listen(3000, 'localhost' );
答案 0 :(得分:1)
在表单中添加name
属性:
<form method="post" action="http://127.0.0.1:3000/">
<fieldset>
<label for="username"></label>
<input type="text" name="username" id="username" placeholder="username">
<label for="password"></label>
<input type="password" name="password" id="password" placeholder="password">
<input type="submit" value="Create user">
</fieldset>
</form>
有关详细信息,请参阅此答案:HTML input - name vs. id