2017-08-30 4 views
0

Node.jsを使ってAPI Restを簡単に作成できます。サーバーはエラーなしで実行され、データベースにはsuccefullを接続しますが、残りのAPIを呼び出すと応答はありません。GET/tvshowできません。

他の質問私は人々がルートを設定するのを忘れているのを見ますが、このコードではすべてのルートを設定してデータを呼び出します。

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); 
     }) 
    }); 
}; 

、すべてのルートを設定し忘れてますが、このコードでは、yがルートに

を送信

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

そして私は、REST呼び出ししよう:

エラー

Cannot POST /tvshow

しかし、私はその理由を理解していない、私が間違って何をやって?あなたが直接使用する場合

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

だからあなたは/api/tvshows

を呼び出すか、する必要があります

+0

あなたのクライアント依頼も投稿できますか?あなたは何のURLを使っていますか? – QoP

+0

Lol、 'app.use( '/ api'、tvshows);'を実行するとhttp:// localhost:3000/tvshows –

+0

と呼ばれ、 'api 'を追加するのを忘れます。' tvshows'ルータをマッピングしています'/ api'の後に'/tvshows'ルートを 'localhost:3000/api/tvshows'で利用できるようにします。 – QoP

答えて

0

/tvshowsはあなたがここに/api接頭辞tvshowsルートを使用し、/tvshowへのルートがありません/tvshows、このようなルータを使用してください:

app.use('/', tvshows); 

use examples on the documentationを参照してください。

0

+0

しかし、tvshowの唯一のモデルが私はコールが必要ですか? –

+0

投稿しようとする場所にコードを投稿できますか? –

+0

ええ、すでにコードを投稿していただき、ありがとうございます。 –