我正在尝试在服务器上发布一些数据,但不起作用,因为我得到类似{' {\ n" rate" :" 26" \ n}':'' }。
---- App.js
const express = require('express'),
mongoose = require('mongoose'),
bodyParser = require('body-parser');
const db = mongoose.connect('mongodb://localhost/invoiceAPI');
app = express(),
port = process.env.PORT || 3000,
Rate = require('./models/rateModel'),
Client = require('./models/clientModel'),
Invoice = require('./models/invoiceModel');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
const invoiceRouter = require('./routes/invoiceRoutes')(Invoice),
clientRouter = require('./routes/clientRoutes')(Client),
rateRouter = require('./routes/rateRoutes')(Rate);
app.use('/api/invoices', invoiceRouter);
app.use('/api/clients', clientRouter);
app.use('/api/rates', rateRouter);
app.listen(port, () => {
console.log('Gulp is running on port ' + port);
});
--- rateRoutes文件:
const express = require('express');
const rateRoutes = (Rate) => {
const rateRouter = express.Router();
rateRouter.route('/')
.get((req,res) => {
Rate.find((err,rates) => {
if(err){
res.status(500).send(err);
}
else res.json(rates);
});
})
.post((req,res) => {
console.log(req.body);
let rate = new Rate(req.body);
rate.save();
res.status(201).send(rate);
});
return rateRouter;
}
module.exports = rateRoutes;
当我用{" rate":26}执行帖子时,我得到了 {' {\ n" rate" :" 26" \ n}':'' }
我读了一些关于bodyParser顺序的内容,但是没有用。