簡體   English   中英

使用Restful / Resourceful / Flatiron獲取查詢字符串參數

[英]Get query string params using Restful / Resourceful / Flatiron

我有以下使用Restful / Resourceful / Flatiron的節點應用程序:

app.js

var flatiron    = require('flatiron'),
    fixtures    = require('./fixtures'),
    restful     = require('restful'),
    resourceful = require('resourceful');

var app = module.exports = flatiron.app;
app.resources = {};
app.resources.Creature = fixtures.Creature;

app.use(flatiron.plugins.http, {
  headers: {
    'x-powered-by': 'flatiron ' + flatiron.version
  }
});
app.use(restful);
app.start(8000, function(){
  console.log(app.router.routes);
  console.log(' > http server started on port 8000');
  console.log(' > visit: http://localhost:8000/ ');
});

這是燈具模塊:

Fixtures.js

var fixtures = exports;

var resourceful = require('resourceful');

// // Create a new Creature resource using the Resourceful library //
fixtures.Creature = resourceful.define('creature', function () {

   var self = this;

   this.restful = true;

   this.all = function (callback) {
     console.log(this);
     callback(null, "ok");   };

 });

如何訪問請求/查詢字符串參數? 例如,如果路線是/ creatures?foo = bar

從Github存儲庫中碰到了這個問題 ,但是評論暗示可能會有更漫長的獲取數據的方法?

我一直在尋找源代碼,以獲取更多信息,但沒有一個明確的方法。 這是有問題的行:

https://github.com/flatiron/resourceful/blob/master/lib/resourceful/resource.js#L379

通過NPM軟件包管理器列出的默認版本已過時,這引起了一些混亂。

在此處查看Github問題:

https://github.com/flatiron/restful/issues/33

將package.json與NPM安裝結合使用可使用以下版本組合:

"restful": "0.4.4",
"director": "1.1.x",
"resourceful": "0.3.x",
"director-explorer": "*"

在此更新版本中,URL格式現在可以采用以下樣式:

/ create / find?foo = bar

有問題的方法可以在這里找到:

https://github.com/flatiron/restful/blob/master/lib/restful.js#L506

在編寫本文時,該方法如下所示:

  router.get('/' + entity + '/find', function () {
    var res = this.res,
        req = this.req;
    preprocessRequest(req, resource, 'find');
    resource.find(req.restful.data, function(err, result){
      respond(req, res, 200, entity, result);
    });
  });

關鍵組成部分req.restful.data是已解析的查詢字符串數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM