所以我制作了一个RESTful API,我希望能够接收原始json数据(这样我也可以将我的API用于移动设备),然后从那里将数据保存到我的数据库(mongoDB)中。
但是你看,有一个问题我似乎无法解决,那就是我无法将原始数据用作json。
代码很简单
const express = require('express') const bodyParser = require('body-parser') const app = express(); app.use(bodyParser.raw({inflate:true, limit: '100kb', type: 'application/json'}); app.post('/post', function(req, res){ res.send(parse.JSON(req.body)); //to convert it to json but this doesn't seem to work })
另外,我正在使用邮递员以原始格式发送请求
const express = require('express') const bodyParser = require('body-parser') const app = express(); app.use(bodyParser.raw({inflate:true, limit: '100kb', type: 'application/json'}); app.post('/post', function(req, res){ res.send(JSON.parse(req.body)); })
在res.send中,必须使用JSON。parse()而不是parse。JSON()将抛出错误,因为它不正确
如果你想了解更多关于JSON的信息。你可以访问 mdn docs