im正在使用Node.js創建API Rest簡單,服務器運行時沒有錯誤,並且連接成功與數據庫,但是當我調用其餘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
但我不明白其中的道理,我做錯了什麼?
你可以發佈你的客戶請求嗎?你使用什麼網址? – QoP
大家好,我忘了添加'api',當你這樣做時,我調用了http:// localhost:3000/tvshows –
,'app.use('/ api',tvshows);',你正在映射'tvshows'路由器在'/ api'之後,所以'/ tvshows'路由在'localhost:3000/api/tvshows'中可用。 – QoP