Skip to content

Commit f253ea2

Browse files
authored
fix: support EGG_OPTIONS_PATH_TO_REGEXP_MODULE env (#292)
1 parent 7a255e8 commit f253ea2

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/egg.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,23 @@ class EggCore extends KoaApplication {
3131
constructor(options = {}) {
3232
options.baseDir = options.baseDir || process.cwd();
3333
options.type = options.type || 'application';
34-
if (typeof options.pathToRegexpModule === 'string') {
34+
const pathToRegexpModule = options.pathToRegexpModule || process.env.EGG_OPTIONS_PATH_TO_REGEXP_MODULE;
35+
if (typeof pathToRegexpModule === 'string') {
3536
/**
3637
* Usage:
38+
*
3739
* ```js
3840
* const app = new Application({
3941
* pathToRegexpModule: '/path/to/path-to-regexp-v8',
4042
* });
4143
* ```
44+
*
45+
* Or you can use `EGG_OPTIONS_PATH_TO_REGEXP_MODULE` environment variable.
46+
* ```bash
47+
* EGG_OPTIONS_PATH_TO_REGEXP_MODULE=/path/to/path-to-regexp-v8 npm start
48+
* ```
4249
*/
43-
options.pathToRegexpModule = require(options.pathToRegexpModule);
50+
options.pathToRegexpModule = require(pathToRegexpModule);
4451
}
4552

4653
assert(typeof options.baseDir === 'string', 'options.baseDir required, and must be a string');

test/utils/router-with-pathToRegexpModule.test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
const assert = require('assert');
22
const request = require('supertest');
3+
const mm = require('mm');
34
const utils = require('../utils');
45

56
describe('test/utils/router-with-pathToRegexpModule.test.js', () => {
7+
afterEach(mm.restore);
8+
69
let app;
710
before(() => {
811
app = utils.createApp('router-app-with-pathToRegexpModule', {
@@ -329,8 +332,9 @@ describe('test/utils/router-with-pathToRegexpModule.test.js', () => {
329332
});
330333
});
331334

332-
describe('router middleware', () => {
335+
describe('set by env: EGG_OPTIONS_PATH_TO_REGEXP_MODULE', () => {
333336
before(() => {
337+
mm(process.env, 'EGG_OPTIONS_PATH_TO_REGEXP_MODULE', 'path-to-regexp-v8');
334338
app = utils.createApp('router-in-app');
335339
app.loader.loadAll();
336340
return app.ready();

0 commit comments

Comments
 (0)