2

Express.js を使用して小さな Node アプリをserver.js構築しています。ファイルをできるだけきれいに保つために、外部ファイルで構成を構築したいと考えています。サーバーの外観は次のとおりです。

// server.js
var express = require( 'express' );
var app = express();
app.enable( 'trust proxy' );

// Set application config params
require( './config.js' )( app, express );

// Load routes and start listening...
require( './routes' )( app );
app.listen( app.get( 'port' ) );

私のファイルはいくつかのデフォルトを設定してから、特定の構成関数で構成config.jsを更新または上書きします。NODE_ENVその厄介なタイミングを除いて、すべてがうまくいくでしょう.

私のルートは、いくつかの設定値にアクセスする必要があります。ルートがロードされ、構成が完全にロードされたにのみサーバーがリッスンを開始するようにする方法はありますか? より良い方法はありますか?

私はイベント ループを取得しますが、私は node/express の初心者であるため、ほぼすべてのことに対してオープンです。さまざまな記事やドキュメンテーション ソースで読んだ経験に基づいて、自分がやりたいとわかっていることを組み合わせて、これを作り上げているようなものです。あまりにも的外れだとは思いませんが、楽観的すぎるのかもしれません。

アップデート

私のconfig.js

module.exports = function( app, express ) {
  var config = this;

  app.configure( function() {
    app.set( 'port', 3000 );
    app.set( 'datasources',   {
      'api'   : {...},
      'mysql' : {...}
    });
    app.use( express.logger() );
    app.use( express.bodyParser() );
    app.use( express.cookieParser() );
    app.use( express.methodOverride() );
    app.use( app.router );
  });

  // dev-specific config
  app.configure( 'development', function() {
    console.log( 'Loading development configuration' );

    app.use( express.errorHandler({ dumpExceptions: true, showStack: true }) );

    // update the mysql config with a connection object
    var datasources = app.get( 'datasources' );
    var mysqlConnection = require( 'mysql' ).createConnection({...});
    datasources.mysql.connection = mysqlConnection;

    app.set( 'datasources', datasources );
  });

  // stg-specific config
  app.configure( 'staging', function() {
    console.log( 'Loading staging configuration' );

    app.use( express.errorHandler() );

    // update the mysql config with a connection object
    var datasources = app.get( 'datasources' );
    var mysqlConnection = require( 'mysql' ).createConnection({...});
    datasources.mysql.connection = mysqlConnection;

    app.set( 'datasources', datasources );
  });


  // prd-specific config
  app.configure( 'production', function() {
    console.log( 'Loading production configuration' );
    app.use( express.errorHandler() );
  });


  console.log( app.get( 'datasources' ) );
  console.log( 'Configuration loaded' );

  return config;
};
4

3 に答える 3

2

config.jsモジュールにコールバックを割り当て、次のようにします

require( './config.js' )( app, express, finish );
var finish = function(){
    // Load routes and start listening...
   require( './routes' )( app );
   app.listen( app.get( 'port' ) );
}

そして、configメソッドで、asyncのようなモジュールを使用し、最後にすべての読み込みを同期させて、関数をコールバックします。例:

**config.js**
module.exports = function(app,express,finish){
    function loadConfig1(cb){
        fs.readFile("....", function(){ // Or someother else async function
              ...  if succesfull, cb(null);
              ...  if fail, cb("error!!!!");
         });
    }
    ....
    function complete(err, results){
         if (!err){
            finish(); // you guarantee that all the functions are completed succesfully
         }
    }
}
于 2013-01-24T18:31:34.287 に答える
-1

私は現在、純粋な json 外部ルーティング構成を可能にする Express の拡張に取り組んでいます。近い将来、このプロジェクトをオープンソース化する予定です。興味のある方は、Github と NPM を介してこのプロジェクトの早期プレビューを共有できれば幸いです。

module.exports = {
  /**
   * settings
   */
  config:{
    "controller_directory" : "routes"
  },

  /**
   * defaults to ./routes directory
   */
  filters: {
    secure: "secure.securityListener",
    global:"secure.globalListener"
  },
  /**
   * defaults to ./routes directory
   * order matters for filters
   * filters should always contain a path which calls next();
   * router should be the final destination to a request
   *
   * possible verbs are [use, all, get, post, delete ...]
   * "use" is the default, "mount" path is stripped and is not visible to the middleware function.
   * other verbs will not strip out the "mount" path
   */
  routes: {
    global: {"filters": ["global"]},
    index:{path:"/", "controller": "index"},
    users:{path:"/users", "controller": "users", "filters": ["secure"]},
    prices:{path:"/prices", "controller": "prices"},
    software:{path:"/software", "controller": "software"},
    auth:{path:"/auth", "controller": "secure.onAuthentication", "verb": "get"}
  }

};
于 2014-06-24T17:13:06.653 に答える