gpt4 book ai didi

javascript - Node js发布request.body未定义

转载 作者:太空宇宙 更新时间:2023-11-04 03:06:58 24 4
gpt4 key购买 nike

服务器.js

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var session = require('express-session');
var fs = require('fs');
var router = require('./router/main')(app, fs);

var server = app.listen(3000, function(){
console.log('Express server has started on port 3000');
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded());

路由器/main.js

module.exports = function(app, fs)
{
app.post('/addUser/', function(req, res){
console.log(req.body);
});
}

我使用 postman 工具!!请求参数 {"password": "pass","name": "dltlsdn"}

但是... req.body 未定义...为什么..??

最佳答案

重新排序 app.use 语句。早些时候,您在通过正文解析器之前处理请求。

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var session = require('express-session');
var fs = require('fs');

//body parser before routes
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());

var router = require('./router/main')(app, fs);

var server = app.listen(3000, function(){
console.log('Express server has started on port 3000');
});

希望对您有帮助。

关于javascript - Node js发布request.body未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37202363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com