Skip to content

Commit

Permalink
src/open router handler for user.
Browse files Browse the repository at this point in the history
  • Loading branch information
bshy522 committed Sep 8, 2016
1 parent 048860a commit 879d247
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/server/middleware.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import { Router } from 'express';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
Expand All @@ -6,12 +7,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;
Expand All @@ -30,6 +32,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));
});
Expand Down
12 changes: 12 additions & 0 deletions src/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {};
}

0 comments on commit 879d247

Please sign in to comment.