2016-03-06 65 views
0

我目前正在開發一個Ionic應用程序並使用Express在NodeJS中編寫服務器並將其託管在Heroku上。但是,它不能正確發佈所需的路線。我得到這個錯誤,當我測試它在Chrome:無法在ExpressJet上的NodeJS服務器上發佈參數id

Failed to load resource: the server responded with a status of 404 (Not Found) https://[SERVER NAME].herokuapp.com/rooms//messages 

服務器應張貼ID參數/間之間/和/消息/,但事實並非如此。以下是服務器端代碼:

 var messages = []; //make an array to hold messages 
    var rooms = []; 

    app.get('/rooms', function(req, res) { 
     res.json(rooms); 
    }); 

    app.get('/rooms/:id/', function(req, res){ 
     var room = rooms[req.params.id]; 
     res.json(room); 
    }); 

    app.post('/rooms', function(req, res) { 
     var newRoom = { 
      timestamp: new Date() 
      //username: req.body.username 
     }; 
     rooms.push(newRoom); 
     res.json(rooms); 
     console.log(rooms); 
    }); 



    app.get('/messages', function(req, res) { //req = request, res = response 
     res.json(messages); 
    }); 

    app.get('/messages/:id', function(req,res) { 
     var message = messages[req.params.id]; 
     res.json(message); 
    }); 

    app.post('/messages', function(req,res){ 
     var newMessage = { 
      message:req.body.message, 
      username:req.body.username, 
      timestamp: new Date() 
     }; 

     messages.push(newMessage); 
     res.json(messages); 
    }); 

爲什麼不正確發佈正確的路由?當我檢查Heroku上的日誌時,它看起來並沒有得到房間的ID: 謝謝。

編輯:下面是進一步參考GitHub的回購協議: 客戶:https://github.com/dukeeagle/ttt-client 服務器:https://github.com/dukeeagle/ttt-server

+0

'的https:// [服務器名稱] .herokuapp.com /間// messages'是正確的請求?看看'/ rooms // messages' –

+0

@DmitriyLoskutov你是什麼意思?當然,[SERVER NAME]是一個佔位符。當我嘗試導航到'/ rooms // messages'時,什麼都沒有出現。 –

+0

你必須聲明'/ rooms /:id/messages'路線爲'app.get('/ rooms /:id/messages',...'。然後嘗試導航到'/ rooms/123/messages '並且你必須得到正確的迴應 –

回答

1

我修好了! 下面是那些有興趣誰正確的路由方法:

app.get('/rooms', function(req, res) { //req = request, res = response 
     var user = users[req.params.id]; 
     var userRooms=[]; 

     res.json(rooms); 
    }); 

    app.get('/rooms/:id', function(req,res) { 
     var room = rooms[req.params.id]; 
     res.json(room); 
    }); 

    app.post('/rooms', function(req,res){ 
     var newRoom = { 
      name:req.body.name, 
      id:rooms.length, 
      username:req.body.username, 
      timestamp: new Date(), 
      messages: [], 
      players: [] 
     }; 
     rooms.push(newRoom); 
     res.json(rooms); 
    }); 

    app.post('/rooms/:id/messages', function(req, res){ 
     var room = rooms[req.params.id]; 
     var newMessage = { 
      username:req.body.username, 
      timestamp: new Date(), 
      message: req.body.message 
     } 
     room.messages.push(newMessage); 
     res.json(room); 
    }); 

    app.post('/rooms/:id/players', function(req, res){ 
     var room = rooms[req.params.id]; 
     var newPlayer = { 
      timestamp: new Date(), 
      player: req.body.username 
     }; 
     room.players.push(newPlayer); 
     res.json(room); 
    }); 
1

我INIT表達與代碼服務器:

var express = require('express'); 
var app = express(); 

app.get('/rooms/:id/messages', function (req, res) { 
    var id = req.params.id; 
    res.send('Hello, moto! ' + id); 
}); 

app.listen(2345, function() { 
console.log('server started'); 
}); 

當我瀏覽到http://localhost:2345/rooms/123/messages我在看頁面字符串Hello, moto! 123

嘗試刪除您的其他路由進行測試,也許它是得到一些衝突。

+0

這不是我所想的找到的應用程序必須導航到'http:// localhost:8100 /#/ rooms/0 /'並查看發送到該特定房間的所有消息的列表。問題是:房間的id –

+0

'app.get'或'app.post'中的第一個參數應該是'path'而不是'hash'的請求URL。在你的情況下路徑是''/''。 ,你可以使用'app.get('/',...',然後解析ori的哈希部分ginalURL。查看更多關於它的信息http://expressjs.com/zh/4x/api.html#router.METHOD –

+0

我不確定我關注。你是指什麼路徑或散列?你鏈接的文件沒有提到這兩件事。 –

相關問題