Node.js 使用Rest客户端、Express和NodeJs发送post请求时出错
Router.js文件 这是通过Rest客户端传递json数据的正确方法,Rest客户端发送application/json类型的对象Node.js 使用Rest客户端、Express和NodeJs发送post请求时出错,node.js,express,mean-stack,Node.js,Express,Mean Stack,Router.js文件 这是通过Rest客户端传递json数据的正确方法,Rest客户端发送application/json类型的对象 const express = require('express') const routing = express.Router() const flightMethods = require("../model/users") //implement routing as per the given requirement routing.post('/bo
const express = require('express')
const routing = express.Router()
const flightMethods = require("../model/users")
//implement routing as per the given requirement
routing.post('/bookFlight', (req, res, err, next) => {
flightBookingObj = JSON.parse(req.body);
flightMethods.bookFlight(flightBookingObj).then((id) => {
return res.status(201).json({"message": `Flight booking is successful with booking id ${id}`})
})
if(err){
return next(err);
}
})
module.exports = routing;
FlightBookingObj定义为
{"customerId": "P1001",
"bookingId": 2001,
"noOfTickets": 3,
"bookingCost": 1800,
"flightId":undefined
}
此路由的POST请求在REST客户端上出现错误。下面是一段代码,用于使用Express在NodeJs中处理POST请求 main.js
class FlightBooking {
constructor(obj) {
this.customerId = obj.customerId;
this.bookingId = obj.bookingId;
this.noOfTickets = obj.noOfTickets;
this.bookingCost = obj.bookingCost;
this.flightId = obj.flightId;
}
}
routes.js
const express = require('express');
const routing = require('./routes.js');
const app = express();
app.use('/', routing);
var server = app.listen(3000, function(){
});
错误是什么样子的?。。。。发布错误消息!错误:无法过帐/预订航班
const express = require('express');
var routing = express.Router();
routing.route('/bookFlight').post(function(req, res, next)
{
var flightBookingObj = req.body.flight;
// Assuming your post request has an object called flight in its body
});
module.exports = routing;