简体   繁体   中英

POST request is empty using ExpressJS

When I do a POST request to my server's route, the request.body is empty. And I'm using body parser to obtain JSON data. What I'm doing wrong?

On the _saveTicket function, I have console.log to show all the body request parameters. And when I do a POST request, request.body is empty.

What I need to parse it automatically ?

server.js

/**
 * Declaration
 **/
var     express   = require('express'),
        database  = require('./config/database.js'),
        morgan    = require('morgan'),
        port      = 2559,
        bodyPar   = require('body-parser'),
        methodOv  = require('method-override'),
        mongoose  = require('mongoose'),
        fsr       = require('file-stream-rotator'),
        logDirectory = __dirname + '/log',
        favicon   = require ('serve-favicon');
        app       = express();

/**
 * DB Connection
 **/
mongoose.connect(database.mongo);

/**
 * Api definition
 **/
 var accessLogStream = fsr.getStream({
   date_format: 'YYYYMMDD',
   filename: logDirectory + '/access-%DATE%.log',
   frequency: 'daily',
   verbose: false
 })

app.use(express.static(__dirname + '/public'));
app.use(morgan('combined', {stream: accessLogStream}));
app.use(bodyPar.urlencoded({'extended':'true'}));
app.use(bodyPar.json());
app.use(bodyPar.json({ type: 'application/vnd.api+json' }));
app.use(methodOv('X-HTTP-Method-Override'));
app.use(favicon(__dirname + '/public/images/favicon.ico'));

/**
 * Routes section
 **/
require('./routes/ticket.js')(app);

/**
 * Starting server
 **/
app.listen (port)
console.log ("Listening on port: " + port);

model/ticket.js

var mongoose = require('mongoose');

var TicketSchema = new mongoose.Schema({
  title:          String,
  description:    String
}, {versionKey: false});

module.exports = mongoose.model('Ticket', TicketSchema, 'Ticket');

routes/ticket.js

var Ticket = require('../models/ticket');

module.exports = function(app){

   _getAllTickets = function(req, res){
    var query = Ticket.find().lean();

    query.exec(function(err, lst){
      if(err)
        res.send(err);

      res.json(lst);
    });
  };

  _saveTicket = function(req, res){
    console.log(req.body);

    var tckt = new Ticket({
      title: req.body.title,
      description: req.body.description
    });

    tckt.save(function(err){
      if(!err)
        console.log('Ticket creation successful. ');
      else
        console.log('ERROR: ' + err);
    });

    res.send(tckt);
  };

  app.get('/api/tickets/', _getAllTickets);
  app.post('/api/tickets/', _saveTicket);
}

Make sure that the HTTP request has the Content-Type header set to be application/json or x-www-form-urlencoded based on your body-parser middleware definitions. My guess is that its coming through right now as neither of those Content-Type 's

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM