2015-10-18 110 views
-1

我目前正試圖學習使用Node.js & Express實現RESTful API。使用此教程:http://code.runnable.com/U7bnCsACcG8MGzEc/restful-api-with-node-js-express-4Node.js和Express不起作用?

我創造了我的本地驅動器上的每個文件,並試圖使用server.js..However我一直得到一個錯誤的節點上運行的代碼。爲什麼會造成這種情況?

enter image description here

+0

該代碼中沒有用於/請求的根路由器,您需要在server.js中設置一個根路由器。 –

+0

相關代碼必須存在於您的問題中,不僅可通過外部鏈接獲得。這是堆棧溢出要求。 – jfriend00

回答

0

您選擇運行的代碼僅適用於與/api開始,你可以在這裏看到的URL請求路由:

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

最重要的是,它接受的路由是/players/player/:id

router.get('/players', function(req, res, next) { 
    res.json({ players: players.getAllPlayer() }); 
}); 

router.get('/players/:id', function(req, res, next) { 
    var player = players.getPlayerById(req.params.id) 
    res.json(player); 
}); 

對於每一個請求,其中包括上述的路由,它輸出方法和url到console.log

即使無法獲得/,您應該在控制檯中看到GET /

現在嘗試訪問此網址:0.0.0.0:8080/api/players

它的工作原理,對不對?