Node.js API不返回任何内容(无错误)

时间:2019-04-08 15:19:59

标签: node.js angular mongodb mean-stack

我的API没有引发任何错误(因为它正在控制台上打印Listening on Port 3000),但也没有返回任何东西。当我尝试在邮递员上进行测试时,它显示为Could not get any response

有人可以帮助我弄清楚问题出在哪里吗?代码如下。

PS:我一直在指这个CRUD operations tutorial

这是我的文件结构:

enter image description here

server.js

const express = require('express'),
    path = require('path'),
    bodyParser = require('body-parser'),
    cors = require('cors'),
    mongoose = require('mongoose');
    const resortRoute = require('./routes/resort_routes');
    mongoose.Promise = global.Promise

    mongoose.connect('mongodb://localhost/db', { useNewUrlParser: true })  //mongodb://127.0.0.1:27017
    var db = mongoose.connection

    db.on('error', console.error.bind(console, 'Mongodb connection error:'))

    const app = express();
    app.use(bodyParser.json());
    app.use(cors());
    app.use('/resort', resortRoute);
    const port = process.env.PORT || 3000;

    const server = app.listen(function(){
        console.log('Listening on port ' + port);
    });

度假村型号

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

var childSchema = mongoose.Schema({
    name : String,
    price : Number
}, { _id : false });

let Resort = new Schema({
  cover_image : {
      type: String,
      required: true
  },
  price: {
      actual_price: { type: Number, required: true},
      discount_price: {type: Number, required: true}
  },
  themes : { type: Array },
  features : { type : Array },
  food : [childSchema],
  trip_type: { type : Array },
  stay_package : [childSchema],
  additional : [childSchema],
  activities : [childSchema],
  name : {
      type: String,
      required: true
  },

  excerpt : {
      type: String,
  },

  description : {
      type: String,
      required: true
  },
  destinationId : { type: mongoose.Schema.Types.Mixed, ref: 'Destination', required: true },
  itinerary : { type: mongoose.Schema.Types.Mixed },
  maps : {
      type : String,
      // required: true
  },
  address : {
      type : String,
      // required: true
  },


  distance_from : {
      location : String,
      distance : Number,
  }
},{
    collection: 'resort'
});


module.exports = mongoose.model('Resort', Resort);

resort_routes.js

const express = require('express');
const app = express();
const resortRoutes = express.Router();


let Resort = require('../models/Resort');
resortRoutes.route('/create').post(function (req, res) {
  console.log(req.body)
  let resort = new Resort(req.body);
  resort.save()
    .then(resort => {
      res.status(200).json({'resort': 'resort added'});
    })
    .catch(err => {
    res.status(400).send("unable to save to database");
    });
});

module.exports = resortRoutes;

0 个答案:

没有答案