我现在已经关注了几次不同的在线尝试,并且我一直在为我的帖子数据而未定义,而console.log(JSON.stringify(req.body))也没有返回任何内容。所以我在某处出错了。 ..
HTML:
<input type="file" id="files" />
<div id="output"></div>
JS:
<!DOCTYPE HTML>
<html>
<head>
<title>Chat</title>
</head>
<body>
<form action="/" method="post">
<button>Close</button><br/><br/>
<label for="username">Your Name: *</label><br/>
<input id="username" type="text" value="" name="username" autocomplete="off" required="required" /><br/>
<!-- <label for="email">Email: *</label><br/>
<input id="email" value="" name="email_address" autocomplete="off" required="required" /><br/> -->
<label for="phone">Phone:</label><br/>
<input id="phone" value="" name="phone" autocomplete="off" /><br/>
<label for="question">Question: </label><br/>
<textarea id="question" name="question">
</textarea required="required"><br/><br/>
<button type="submit">Chat</button>
</form>
</body>
</html>
答案 0 :(得分:1)
尝试使用express-fileupload包,您可以在req.body中获取数据
var app = require('express')();
var http = require('http').Server(app);
const fileUpload = require('express-fileupload')
app.use(fileUpload());
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.post('/', function(req, res) {
var username = req.body.username;
res.send('<h1>Hello</h1> '+username);
});
http.listen(3000, function(){
console.log('listening on *:3000');
});