无法GET / tvshow

时间:2017-08-30 14:03:09

标签: node.js rest express

我正在使用Node.js创建API Rest简单,服务器运行时没有错误并且连接成功与数据库,但是当我调用其余的api时,这不响应任何。

我还有其他问题我看到人们忘了设置一条路线,但在这段代码中我设置了所有路线并调用了数据。

App.js:

var express         = require("express"),
    app             = express(),
    bodyParser      = require("body-parser"),
    methodOverride  = require("method-override"),
    mongoose        = require('mongoose');

// Connection to DB
mongoose.connect('mongodb://localhost/tvshows', function(err, res) {
  if(err) throw err;
  console.log('Connected to Database');
});

// Middlewares
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());

// Import Models and controllers
var models     = require('./models/tvshow')(app, mongoose);
var TVShowCtrl = require('./controllers/tvshows');


var router = express.Router();
router.get('/', function(req, res) {
  res.send("Hello world!");
});
app.use(router);

// API routes
var tvshows = express.Router();

tvshows.route('/tvshows')
  .get(TVShowCtrl.findAllTVShows)
  .post(TVShowCtrl.addTVShow);

tvshows.route('/tvshows/:id')
  .get(TVShowCtrl.findById)
  .put(TVShowCtrl.updateTVShow)
  .delete(TVShowCtrl.deleteTVShow);

app.use('/api', tvshows);

// Start server
app.listen(3000, function() {
  console.log("Node server running on http://localhost:3000");
});

控制器/ tvshows.js:

//File: controllers/tvshows.js
var mongoose = require('mongoose');
var TVShow  = mongoose.model('TVShow');

//GET - Return all tvshows in the DB
exports.findAllTVShows = function(req, res) {
    TVShow.find(function(err, tvshows) {
    if(err) res.send(500, err.message);

    console.log('GET /tvshows')
        res.status(200).jsonp(tvshows);
    });
};

//GET - Return a TVShow with specified ID
exports.findById = function(req, res) {
    TVShow.findById(req.params.id, function(err, tvshow) {
    if(err) return res.send(500, err.message);

    console.log('GET /tvshow/' + req.params.id);
        res.status(200).jsonp(tvshow);
    });
};

//POST - Insert a new TVShow in the DB
exports.addTVShow = function(req, res) {
    console.log('POST');
    console.log(req.body);

    var tvshow = new TVShow({
        title:    req.body.title,
        year:     req.body.year,
        country:  req.body.country,
        poster:   req.body.poster,
        seasons:  req.body.seasons,
        genre:    req.body.genre,
        summary:  req.body.summary
    });

    tvshow.save(function(err, tvshow) {
        if(err) return res.send(500, err.message);
    res.status(200).jsonp(tvshow);
    });
};

//PUT - Update a register already exists
exports.updateTVShow = function(req, res) {
    TVShow.findById(req.params.id, function(err, tvshow) {
        tvshow.title   = req.body.petId;
        tvshow.year    = req.body.year;
        tvshow.country = req.body.country;
        tvshow.poster  = req.body.poster;
        tvshow.seasons = req.body.seasons;
        tvshow.genre   = req.body.genre;
        tvshow.summary = req.body.summary;

        tvshow.save(function(err) {
            if(err) return res.send(500, err.message);
      res.status(200).jsonp(tvshow);
        });
    });
};

//DELETE - Delete a TVShow with specified ID
exports.deleteTVShow = function(req, res) {
    TVShow.findById(req.params.id, function(err, tvshow) {
        tvshow.remove(function(err) {
            if(err) return res.send(500, err.message);
      res.status(200);
        })
    });
};

在类似的问题中,所有忘记设置路线,但在此代码中发送路线

  

app.use('/ api',tvshows);

然后我尝试调用REST:

  

                            错误                       

Cannot POST /tvshow
       

但我不明白原因,我做错了什么?

2 个答案:

答案 0 :(得分:0)

/tvshow没有路由,只有/tvshows

答案 1 :(得分:0)

您在这里使用tvshows前缀为/api的路由:

app.use('/api', tvshows);

所以你应该致电/api/tvshows

或者,如果您想直接使用/tvshows,请使用如下路由器:

app.use('/', tvshows);

请参阅use examples on the documentation