有没有办法 bootstrap 一个 Express 应用程序?
Is there a way to bootstrap an Express app?
我正在 Express 中构建一个应用程序,但我希望它在服务器实际启动之前调用 S3 来检索一些密钥。这在 Express 中可能吗?如果我 google bootstrap Express
我通过 Twitter Bootstrap 设置 Express 获得点击。
我以前使用过 Sails.js,您可以在 bootstrap.js 文件中指定 bootstrap 配置,所以我想我正在寻找类似的东西。否则还有其他选择吗?
我有一个 index.js 文件和一个调用 index.js 文件的单独 bin/www 文件。我希望 bootstrapping 在 index.js 中完成,以便它包含在测试中。现在我 'initialize' bootstrap 但由于它是异步的,服务器已经启动并且 运行 在 bootstrap 完成(或出错)之前
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;
将我的解决方案发布给遇到相同问题但头脑一片空白的任何人。我分别保留了 bin/www 和 index.js 文件,但通过一种方法从 index.js 返回了 express 对象。感谢 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);
});
});
});
我正在 Express 中构建一个应用程序,但我希望它在服务器实际启动之前调用 S3 来检索一些密钥。这在 Express 中可能吗?如果我 google bootstrap Express
我通过 Twitter Bootstrap 设置 Express 获得点击。
我以前使用过 Sails.js,您可以在 bootstrap.js 文件中指定 bootstrap 配置,所以我想我正在寻找类似的东西。否则还有其他选择吗?
我有一个 index.js 文件和一个调用 index.js 文件的单独 bin/www 文件。我希望 bootstrapping 在 index.js 中完成,以便它包含在测试中。现在我 'initialize' bootstrap 但由于它是异步的,服务器已经启动并且 运行 在 bootstrap 完成(或出错)之前
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;
将我的解决方案发布给遇到相同问题但头脑一片空白的任何人。我分别保留了 bin/www 和 index.js 文件,但通过一种方法从 index.js 返回了 express 对象。感谢 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);
});
});
});