node.js - 节点/快递: res.body 未定义
问题描述
(我已经阅读了其他类似 OP 的答案,但它们并没有解决我的问题。我已经在使用express.json()
)
在下面的代码中,res.body
是undefined
.
app.use(cors({
origin: '*',
credentials: true,
optionSuccessStatus: 200
}))
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:4200')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization')
res.header('Access-Control-Allow-Methods', 'POST, GET')
next()
})
app.use(cookieParser())
app.use(express.json({ limit: '50mb' }))
app.use(express.urlencoded({ extended: true, limit: '50mb' }))
app.use(bodyParser.urlencoded({ extended: true }))
app.post('/new-data', ensureLoggedIn('/auth/facebook'), (req, res) => {
console.log(res.body) // logs undefined
})
我发送到服务器的是:
fetch('/new-data', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ data })
})
其中data
是一个包含 8 个图像的数组作为 Base64 字符串,每个大约 1.5Mb,我可以在我的 Chrome 网络面板上看到 POST 请求正确发出。
解决方案
因为你得到 res.body ,你应该得到 req.body 。
推荐阅读
- java - 在 Oreo 版本之后获取 CallLogs
- android - 如何为 xxxhdpi 设备获取带有 play store 的模拟器?
- android - 如何在单个活动上水平排列多个图像
- c# - 加密连接字符串在 asp.net C# 中给出了奇怪的错误
- laravel - 是否可以从 Laravel 控制器执行命令提示符?
- docker - docker daemon 选项 --selinux-enabled 有什么作用
- javascript - 在每个“n”索引之后将元素从一个数组插入到另一个数组
- python - 动态创建 simpy.resource
- dart - AssetImage 未在颤振应用程序中显示图像
- bash - 如何在 foreach 循环中正确使用 sed