diff --git a/lib/egg.js b/lib/egg.js index d0f5b52c..93bfb33f 100644 --- a/lib/egg.js +++ b/lib/egg.js @@ -31,16 +31,23 @@ class EggCore extends KoaApplication { constructor(options = {}) { options.baseDir = options.baseDir || process.cwd(); options.type = options.type || 'application'; - if (typeof options.pathToRegexpModule === 'string') { + const pathToRegexpModule = options.pathToRegexpModule || process.env.EGG_OPTIONS_PATH_TO_REGEXP_MODULE; + if (typeof pathToRegexpModule === 'string') { /** * Usage: + * * ```js * const app = new Application({ * pathToRegexpModule: '/path/to/path-to-regexp-v8', * }); * ``` + * + * Or you can use `EGG_OPTIONS_PATH_TO_REGEXP_MODULE` environment variable. + * ```bash + * EGG_OPTIONS_PATH_TO_REGEXP_MODULE=/path/to/path-to-regexp-v8 npm start + * ``` */ - options.pathToRegexpModule = require(options.pathToRegexpModule); + options.pathToRegexpModule = require(pathToRegexpModule); } assert(typeof options.baseDir === 'string', 'options.baseDir required, and must be a string'); diff --git a/test/utils/router-with-pathToRegexpModule.test.js b/test/utils/router-with-pathToRegexpModule.test.js index 79d302f1..b37f6ca4 100644 --- a/test/utils/router-with-pathToRegexpModule.test.js +++ b/test/utils/router-with-pathToRegexpModule.test.js @@ -1,8 +1,11 @@ const assert = require('assert'); const request = require('supertest'); +const mm = require('mm'); const utils = require('../utils'); describe('test/utils/router-with-pathToRegexpModule.test.js', () => { + afterEach(mm.restore); + let app; before(() => { app = utils.createApp('router-app-with-pathToRegexpModule', { @@ -329,8 +332,9 @@ describe('test/utils/router-with-pathToRegexpModule.test.js', () => { }); }); - describe('router middleware', () => { + describe('set by env: EGG_OPTIONS_PATH_TO_REGEXP_MODULE', () => { before(() => { + mm(process.env, 'EGG_OPTIONS_PATH_TO_REGEXP_MODULE', 'path-to-regexp-v8'); app = utils.createApp('router-in-app'); app.loader.loadAll(); return app.ready();