我想将一些数据发布到服务器。问题是,似乎服务器无法接收数据。
所以我的帖子数据是这样的:
name=hello&email=there&message=sometext
我的服务器代码是这样的:
var url = require('url'),
express = require('express'),
http=require('http'),
path = require('path'),
nodemailer = require('nodemailer');
var app = express();
var server = http.createServer(app);
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
res.render('home');
});
app.use(express.bodyParser());
app.post('/', function(req, response){
console.log(req.body);
// console.log(request.body.name);
});
server.listen(4000);
console.log('server running ' + 'now ' + Date.now());
当console.log(reg.body)
运行时,终端输出为“未定义”
答案 0 :(得分:4)
默认情况下app.use(express.bodyParser());
会解析所有查询字符串输出。问题的简单解决方案是尝试记录req.query
,例如
console.log(req.query);
答案 1 :(得分:0)
移动app.use(express.bodyParser()));在app.use之前(express.static(path.join(__ dirname,'public')));