无法在Express上的NodeJS服务器中发布param id

时间:2016-03-06 22:45:14

标签: javascript node.js express heroku ionic-framework

我目前正在开发一个Ionic应用程序,并使用Express在NodeJS中编写服务器并将其托管在Heroku上。但是,它并没有正确发布所需的路线。我在Chrome上测试时出现此错误:

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

服务器应该在/ 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

2 个答案:

答案 0 :(得分:1)

我使用该代码初始化服务器:

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

尝试删除其他测试路线,可能会产生一些冲突。

答案 1 :(得分: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);
    });