Express + postman没有回复

时间:2016-09-20 03:01:17

标签: express postman body-parser

我似乎无法通过Postman& amp;表达。 我的POST动词代码如下

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

var fdbRouter = express.Router();
fdbRouter.route('/films')
//post verb
.post(function (req, res) { 

var item = new Film(req.body);
console.log(item);
//item.save();
res.status(201).send(item);

})

我的邮差设置如下

postman setup

我和谷歌结识并想出了这个  1. Node.js/Express form post req.body not working  2. req.body empty on posts  3. Express + Postman, req.body is empty  4. `express` app - not able to test the `post` request using `postman`

PS 邮递员中观看的项目默认使用我所拥有的mongoose模式,无论表达是否有效,都会创建它。

1 个答案:

答案 0 :(得分:0)

每当我使用快递处理邮件请求时,它的格式如下(假设您正在使用mongoDB):

var express = require('express');
var fdbRouter = express.Router();
var model = require("<FILE TO IMPORT MODEL>") //This file will differ, but you need to import the model you created for films in your DB
var FilmModel = model.Film //also depends on how you exported your model in your dbFiles. Either way you need this model as the blue print for your post request

//I'm also assuming here that you're calling /films correctly and have it sourced as such in your app.js so you don't need /api/films as you have listed in your postman setup image 

fdbRouter.post('/films', function(req,res){
  var newfilm = new FilmModel()
  newFilm.title = req.body.Title
  newFilm.year =  req.body.Year
  newFilm.genre = req.body.Genre
  newFilm.save(function(err, savedObject){
    if(err){
      console.log(err)
      res.status(500).send()
    }else{
      res.send(savedObject)
    }
  })
})

请注意:此代码示例假定已设置具有架构和模型的测试数据库,并且可以将其导入到路由文件中。

请告诉我这是否会让您走上正轨!