我知道这已被多次询问,但我一直在四处寻找,但仍无法找到问题的答案.
这是我的代码,我确保在定义路由之前使用和配置主体解析器.我只使用.json()和bodyParser,因为我现在只测试一个POST函数,但我甚至尝试过app.use(bodyParser.urlencoded({extended:true}));
var express = require('express'),
bodyParser = require('body-parser'),
app = express();
app.use(bodyParser.json());
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'))
});
app.post('/itemSearch', function(req, res) {
//var Keywords = req.body.Keywords;
console.log("Yoooooo");
console.log(req.headers);
console.log(req.body);
res.status(200).send("yay");
});
Run Code Online (Sandbox Code Playgroud)
这是我收到的回复
Node app is running at localhost:5000
Yoooooo
{ host: 'localhost:5000',
connection: 'keep-alive',
'content-length': '146',
'cache-control': 'no-cache',
origin: 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundarynJtRFnukjOQDaHgU',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
'postman-token': '984b101b-7780-5d6e-5a24-ad2c89b492fc',
accept: '*/*', …Run Code Online (Sandbox Code Playgroud) 所以我在server.js文件中有以下代码,我正在运行node.js. 我正在使用express来处理HTTP请求.
app.post('/api/destinations', function (req, res) {
var new_destination = req.body;
console.log(req.body);
console.log(req.headers);
db.Destination.create(new_destination, function(err, destination){
if (err){
res.send("Error: "+err);
}
res.json(destination);
});
});
Run Code Online (Sandbox Code Playgroud)
我在终端中运行以下内容:
curl -XPOST -H "Content-Type: application/json" -d '{"location": "New York","haveBeen": true,"rating": 4}' http://localhost:3000/api/destinations
Run Code Online (Sandbox Code Playgroud)
运行该server.js后打印出以下内容.
{}
{ host: 'localhost:3000',
'user-agent': 'curl/7.43.0',
accept: '*/*',
'content-type': 'application/json',
'content-length': '53' }
Run Code Online (Sandbox Code Playgroud)
所以req.body是{}.我阅读了其他关于类似问题的Stack Overflow帖子,其中由于正文解析器,内容类型不正确.但这不是问题,因为内容类型是application/json.
任何想法如何获得请求的实际主体?
提前致谢.
我已经尝试了其他一些 stackoverflow 帖子中的所有解决方案,但没有解决我的问题。
这是我的 app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
var index = require('./routes/index');
var v1 = require('./routes/route');
app.use('/', index);
//routes for api
app.use('/v1',v1);
Run Code Online (Sandbox Code Playgroud)
这是我的post控制器 …
我有以下表单并提交结果为空req.body这是我的HTML:
<form action="/contact" name="contactUs" id="contactUs" method="POST">
<label for="email">Email Message</label>
<input type="text" name="email" id="email">
<input type="submit" value='Save'>
</form>
Run Code Online (Sandbox Code Playgroud)
这是我的(部分)server.js:
app.use(bodyParser.json());
app.use("/", expressStaticGzip("dist"));
app.post("/contact", (req, res) => {
console.log('anyone there?')
res.json(req.body);
});
Run Code Online (Sandbox Code Playgroud)
该console.log工程的罚款.我得到一个空的对象req.body.