在Express中未定义的req.body

时间:2019-05-24 15:47:24

标签: node.js express body-parser

我正在开发一个平均堆栈项目,一切正常。但是我的问题是,当我从有角度的角度使用我的邮政服务时,对于邮政休息服务我始终没有定义req.body,但是当我在邮递员上测试我的服务时一切正常。 这是我的app.js

var express = require('express');
const bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var mongoose = require('mongoose');
var bluebird = require('bluebird');
const cors = require('cors');
var api = require('./routes/api/api.route');
var tmp = require('./routes/api/templates.route');



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");
  res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
  next();
});
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/api', api);
app.use('/tmp', tmp);



//connect to bd
mongoose.connect('mongodb://127.0.0.1:27017/exportModule',{ useNewUrlParser: true })
  .then(() => { console.log(`Succesfully Connected to the Mongodb Database  at URL : mongodb://127.0.0.1:27017/exportModule`) })
  .catch(() => { console.log(`Error Connecting to the Mongodb Database at URL : mongodb://127.0.0.1:27017/exportModule`) })



// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

0 个答案:

没有答案