0

さまざまなバックエンド サービスへの GET および POST http 要求を作成するいくつかのエンドポイントを作成しようとしています。データの形式はすべて非常に似ているため、responseHandler 関数はさまざまなルート関数に何度もコピーされます。再利用のために responseHandler を外部化する方法がある場合。私はそれを移動しようとしましたが、そうすると res への参照が失われます。よりモジュール化された設計に関するヒントはありますか?

routes['/endpoint'] = function(req, res){
    console.log("Serving endpoint: /endpoint")
    var params={"param": "param-value"}

    var options = {
      host: 'localhost',
      path: '/service?param='+params.param,
      method: 'GET'
    };

    var responseHandler = function(response) {
      var data = '';

      // keep track of the data you receive
      response.on('data', function(chunk) {
        data += chunk + "\n";
      });

      // finished? ok, send the data to the client in JSON format
      response.on('end', function() {
            res.header("Content-Type:","application/json");
            res.end(data);
      });
    };

    // make the request, and then end it, to close the connection
    http.request(options, responseHandler).end();
};
4

2 に答える 2

0

一般的に、私はあなたがresponseHandlersと呼ばれるあなたのlibにフォルダを作成し、次のようなものを含むファイルを追加することができると思います

var responseHandler = function(response) {
  var data = '';

  // keep track of the data you receive
  response.on('data', function(chunk) {
    data += chunk + "\n";
  });

  // finished? ok, send the data to the client in JSON format
  response.on('end', function() {
        res.header("Content-Type:","application/json");
        res.end(data);
  });
};
exports.Handler = responseHandler;

それをwhateverHandler.jsとして保存してから、whatever.jsを必要とするindex.jsファイルを作成し、それをHandlerにエクスポートします。このように、将来さらにハンドラーを追加する必要がある場合は、ファイルを追加してindex.jsを更新するだけです。使用するには、ルートハンドラで次のようなことを行います

var handler = require('./lib/responseHandlers').whateverHandler;
routes['/endpoint'] = function(req, res){
    console.log("Serving endpoint: /endpoint")
    var params={"param": "param-value"}

    var options = {
      host: 'localhost',
      path: '/service?param='+params.param,
      method: 'GET'
    };
};

// make the request, and then end it, to close the connection
http.request(options, handler).end();
};
于 2012-11-15T17:06:26.253 に答える
0

responseHandler関数ジェネレーターに変えて、resオブジェクトを渡して失わないようにすることができます。

var responseHandler = function(res) {
  return function(response) {
    var data = '';

    // keep track of the data you receive
    response.on('data', function(chunk) {
      data += chunk + "\n";
    });

    // finished? ok, send the data to the client in JSON format
    response.on('end', function() {
          res.header("Content-Type:","application/json");
          res.end(data);
    });
  };
}

そして、次のように使用します。

routes['/endpoint'] = function(req, res){
    console.log("Serving endpoint: /endpoint")
    var params={"param": "param-value"}

    var options = {
      host: 'localhost',
      path: '/service?param='+params.param,
      method: 'GET'
    };

    // make the request, and then end it, to close the connection
    http.request(options, responseHandler(res)).end();
};
于 2012-11-15T17:38:43.600 に答える