From 6469f0f74ce10f3f751d9cb28c693764af87ef03 Mon Sep 17 00:00:00 2001 From: larry Date: Thu, 8 Sep 2016 12:03:58 +0800 Subject: [PATCH] src/open router handler for user. --- src/server/middleware.js | 5 ++++- src/server/utils.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/server/middleware.js b/src/server/middleware.js index f8ef41c85672..e1975f21389a 100644 --- a/src/server/middleware.js +++ b/src/server/middleware.js @@ -6,12 +6,13 @@ import baseConfig from './config/webpack.config'; import loadConfig from './config'; import getIndexHtml from './index.html'; import getIframeHtml from './iframe.html'; -import { getHeadHtml } from './utils'; +import { getHeadHtml, getMiddleware } from './utils'; export default function (configDir) { // Build the webpack configuration using the `baseConfig` // custom `.babelrc` file and `webpack.config.js` files const config = loadConfig('DEVELOPMENT', baseConfig, configDir); + const middlewareFn = getMiddleware(configDir); // remove the leading '/' let publicPath = config.output.publicPath; @@ -30,6 +31,8 @@ export default function (configDir) { router.use(webpackDevMiddleware(compiler, devMiddlewareOptions)); router.use(webpackHotMiddleware(compiler)); + middlewareFn(router); + router.get('/', function (req, res) { res.send(getIndexHtml(publicPath)); }); diff --git a/src/server/utils.js b/src/server/utils.js index 07f67fc9ea91..dba71980c852 100644 --- a/src/server/utils.js +++ b/src/server/utils.js @@ -24,3 +24,15 @@ export function getEnvConfig(program, configEnv) { } }); } + +export function getMiddleware(configDir) { + const middlewarePath = path.resolve(configDir, 'middleware.js'); + if (fs.existsSync(middlewarePath)) { + let middlewareModule = require(middlewarePath); // eslint-disable-line global-require + if (middlewareModule.__esModule) { + middlewareModule = middlewareModule.default; + } + return middlewareModule; + } + return function () {}; +}