Express.js的外部配置 [英] External Configuration of Express.js

查看:84
本文介绍了Express.js的外部配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Express.js构建一个小的Node应用程序,并且为了保持我的server.js文件尽可能整洁,我想在一个外部文件中构建我的配置.服务器外观如下:

I'm building out a small Node app using Express.js and, in an effort to keep my server.js file as clean as possible, I'd like to build my configuration in an external file. Here's how the server looks:

// 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的配置功能中的配置.除了那讨厌的时机,一切都会好起来的.

My config.js file sets a few defaults and then updates or overrides the config in NODE_ENV-specific configuration functions. Everything would be fine except for that pesky timing.

我的路线需要访问某些配置值.有没有一种方法可以确保我的路由已加载并且我的服务器仅在配置完全加载后 才开始侦听?有更好的方法吗?

My routes need access to some of the configuration values. Is there a way to ensure that my routes are loaded and my server starts only starts listening only after the configuration is completely loaded? Is there a better way?

我得到了事件循环,但是我对node/express感到陌生,所以我几乎可以接受任何东西.我会根据我在各种文章或文档来源中所读到的经验,将自己想做的事情汇总在一起,以此来弥补这一点.我认为我在这里并没有太过落伍,但这也许过于乐观了.

I get the event loop, but I am brand-spanking new to node/express so I'm open to pretty much anything. I'm kind of making this up as I go along by cobbling together what I know I'd like to do based on experience with what I read about in various articles or documentation sources. I don't think I'm too off base here, but maybe that's being overly optimistic.

更新

我的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;
};

推荐答案

如果配置中的代码都是同步的,则应该可以:

If the code inside your config is all synchronous this should work:

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

// -----------------------------------------------
// If the code inside your config is all synchronous this call 
// will block, which is what you want.
config(app, express );

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

这篇关于Express.js的外部配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆