【问题标题】:build multiple layer restful API in node.js express在 node.js express 中构建多层 restful API
【发布时间】:2014-03-19 05:30:10
【问题描述】:

我正在使用 node.js express 来构建简单的 rest API,我已经构建了一个类似的 API

app.get('/sites/:site/:building/:floor',function(req,res){
    var building = req.params.building, floor = req.params.floor, site = req.params.site;
    console.log('query site ',site,building, floor);
    .....
}

当客户端在 angular.js 中进行 AJAX 请求时

$http.get('/sites/london')
         .success(function(data) {

        }).error(function(data, status, headers, config) {

        });

服务器不会响应这种 URL,除非我将 express API 更改为

app.get('sites/:site',function(){})
app.get('sites/:site/:building',function(){})
app.get('sites/:site/:building/:floor',function(){}) 

但是上面的方法太繁琐了,不知道有没有可能一句话就建好APIapp.get('sites/:site/:building/:floor',function(){})

【问题讨论】:

    标签: node.js rest express


    【解决方案1】:

    以下stackoverflow answer 应该可以帮助您。但是要回答您的问题,以下应该可以工作

    app.get('sites/:site?/:building?/:floor?',function(){}) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      相关资源
      最近更新 更多