2016-07-27 124 views
0

我正在Express中构建一个应用程序,但我希望它在S3启动之前在服务器启动前检索一些密钥。 Express可以吗?如果我谷歌​​我得到点击设置与Twitter Bootstrap快递。有没有办法启动一个Express应用程序?

我以前使用过Sails.js,你可以在bootstrap.js文件中指定bootstrap配置,所以我想我正在寻找类似的东西。否则有其他选择吗?

我有一个index.js文件和一个单独的bin/www文件,它调用index.js文件。我想在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; 
    }) 
} 

斌/ 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); 
    }); 
    }); 
}); 
相关问题