我正在运行本地服务器,并为用户提供了输入表单。我想要当他们按下提交时仅输出一条消息“谢谢!”。相反,当按下提交时,什么也没有发生,一段时间后,我收到消息,表明服务器已断开连接。有人可以帮我吗?
NodeJS:
var fs = require('fs');
const log=require('simple-node-logger').createSimpleLogger();
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var port = process.env.PORT || 8000;
app.use(express.static(__dirname + '/server'));
app.use(express.static(__dirname + '/public'));
app.use('/images', express.static(__dirname +'/images'));
app.get('/', function(req, res){
res.sendfile('main.html');
});
//app.listen(port, function(){
// console.log('server is running on ' + port);
//});
app.post('/submit', function(req, res){
console.log(req.body.rank);
});
HTML:
<form method="POST" action="/submit" >
<img src="images/Picture1.png" style="width:280px;"/>
<br />
<select name="rank" size="1" style="width:80px;" required>
<option value="" selected disabled hidden>Rank</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<br />
<input type="submit" value="Submit" name="submit" id="submit"/>
</form>
最佳答案
您的邮递员什么都不做
app.post('/submit', function(req, res){
console.log(req.body.rank);
});
您可以执行以下操作,
使用res.redirect在另一条路线上重定向:
app.post('/submit', function(req, res){
console.log(req.body.rank);
// redirect on /
return res.redirect("/");
});
渲染成功的html文件:
app.post('/submit', function(req, res){
console.log(req.body.rank);
// render success
return res.sendfile('success.html');
});
返回json:
app.post('/submit', function(req, res){
console.log(req.body.rank);
// render success
return res.status(200).json({message: "OK"})
});