2016-07-27 4 views
0

私はExpressでアプリケーションを構築していますが、サーバーが実際に起動する前にS3に呼び出していくつかのキーを取得したいと思います。これはExpressで可能ですか?私がgoogleの場合​​私は、TwitterのブートストラップでExpressを設定するヒットを得る。Expressアプリをブートストラップする方法はありますか?

以前はSails.jsを使用していましたが、bootstrap.jsファイルでブートストラップ設定を指定することができましたので、私は似たようなものを探しています。そうでなければ代替手段がありますか?

私はindex.jsファイルとindex.jsファイルを呼び出す別のbin/wwwファイルを持っています。私は、テストの一部として含まれるように、index.jsでブートストラップをしたいと思います。今、私は、ブートストラップを「初期化」は、それは非同期だとサーバがすでに稼働しているブートストラップを持って前に、完全な(またはアウトエラーが発生した)すなわち

import express from 'express'; 
import {initializeFromS3} from './services/initializerService'; 
import healthCheckRouter from './routes/healthCheckRouter'; 
import bodyParser from 'body-parser'; 

initializeFromS3(); // Calls out to S3 and does some bootstrapping of configurations 
const app = express(); 

app.use(bodyParser.json());  // to support JSON-encoded bodies 
app.use(bodyParser.urlencoded({  // to support URL-encoded bodies 
    extended: true 
})); 

// ---------------------------- Routes ---------------------------- 
app.use('/', express.static('dist/client/')); 
app.use('/health-check', healthCheckRouter); 

export default app; 

答えて

0

同じ渡って来て、持っている人のために私の解決策を投稿心の空白。私はbin/wwwとindex.jsファイルを別々に保っていましたが、エクスプレスオブジェクトはメソッドを介してindex.jsから返されました。下の解決策は、フレンドリーな人々Githubのおかげです。

Index.jsファイル:

import express from 'express'; 
import {initialize} from './services/appService'; 
import healthCheckRouter from './routes/healthCheckRouter'; 
import loginRouter from './routes/loginRouter'; 


export function getExpress() { 
    return initialize() 
    .then(() => { 
     const app = express(); 

     // ---------------------------- Routes ---------------------------- 
     app.use('/', express.static('dist/client/')); 
     app.use('/login', loginRouter); 
     app.use('/health-check', healthCheckRouter); 
     return app; 
    }) 
} 

binに/ WWWのファイル:

import winston from 'winston'; 
import bodyParser from 'body-parser'; 

import {getExpress} from '../index'; 

getExpress() 
    .then(app => { 
    app.use(bodyParser.json());  // to support JSON-encoded bodies 
    app.use(bodyParser.urlencoded({  // to support URL-encoded bodies 
     extended: true 
    })); 

    const port = 3002; 
    app.listen(port,() => { 
     winston.info(`Server listening on port ${port}!`); 
    }); 
    }) 
    .catch(err => { 
    winston.error('Error starting server', err); 
    }); 

統合テスト:

import request from 'supertest'; 
import {getExpress} from '../../index' 

describe('/login integration test',() => { 
    let app = null; 

    beforeEach(done => { 
    getExpress() 
     .then(res => { 
     app = res; 
     done(); 
     }); 
    }); 

    describe('GET /login',() => { 
    it('should return 400 error if \'app\' is not provided as a query string', done => { 
     request(app) 
     .get('/login') 
     .expect(400, done); 
    }); 
    }); 
}); 
関連する問題