我正在学习一个教程,以创建一个将数据发送到本地服务器的表单,并且遇到超时错误。这是我第一次使用node.js express应用程序处理帖子请求,但我陷入了困境。
这是车把代码:
form method="POST" action="send">
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="validationServer01">First name</label>
<input type="text" class="form-control is-valid" id="validationServer01" placeholder="First name" value="Mark" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationServer02">Last name</label>
<input type="text" class="form-control is-valid" id="validationServer02" placeholder="Last name" value="Otto" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationServerUsername">Username</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupPrepend3">@</span>
</div>
<input type="text" class="form-control is-invalid" id="validationServerUsername" placeholder="Username" aria-describedby="inputGroupPrepend3" required>
<div class="invalid-feedback">
Please choose a username.
</div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="validationServer03">City</label>
<input type="text" class="form-control is-invalid" id="validationServer03" placeholder="City" required>
<div class="invalid-feedback">
Please provide a valid city.
</div>
</div>
<div class="col-md-3 mb-3">
<label for="validationServer04">State</label>
<input type="text" class="form-control is-invalid" id="validationServer04" placeholder="State" required>
<div class="invalid-feedback">
Please provide a valid state.
</div>
</div>
<div class="col-md-3 mb-3">
<label for="validationServer05">Zip</label>
<input type="text" class="form-control is-invalid" id="validationServer05" placeholder="Zip" required>
<div class="invalid-feedback">
Please provide a valid zip.
</div>
</div>
</div>
<div class="form-group">
<div class="form-check">
<input class="form-check-input is-invalid" type="checkbox" value="" id="invalidCheck3" required>
<label class="form-check-label" for="invalidCheck3">
Agree to terms and conditions
</label>
<div class="invalid-feedback">
You must agree before submitting.
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Submit form</button>
</form>
这是入口点代码:
onst express = require ('express');
const bodyParser = require ('body-parser');
const exphbs = require ('express-handlebars');
const nodemailer = require ('nodemailer');
const path = require ('path');
const app = express ();
//View Engine Setup
app.engine ('handlebars', exphbs());
app.set ('view engine', 'handlebars');
//Static Folder
app.use('/public', express.static(path.join(__dirname, 'public')));
//Body Parser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//route
app.get ('/', (req,res) => {
res.render('contact', {layout:false})
});
//post route for submission
app.post('/send', (req, res) => {
console.log(req.body);
});
app.listen (3000,() => console.log ('Server started...'));
我在服务器上获得的所有内容是:{}当我尝试提交时。
答案 0 :(得分:1)
每当您向服务器发送表单值时,请始终将name
属性添加到输入字段中。否则,您将不会在服务器端获得请求参数(req.body
)。
name
属性在您的情况下用作JSON属性。如果您不提供name
属性,则JSON不会使用字段值进行更新。
花一些时间阅读本教程: https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_and_retrieving_form_data