Send Budo-served requests straight to Budo, bypassing the specified Express app

master
Sven Slootweg 4 years ago
parent a8f630ea3c
commit 640b0aeaa5

@ -57,7 +57,7 @@ function monkeyPatchEnd(res) {
};
}
function createExpressMiddleware(app) {
function createExpressMiddleware(app, fullBundlePath) {
return function expressMiddleware(req, res, next) {
try {
monkeyPatchEnd(res);
@ -67,28 +67,37 @@ function createExpressMiddleware(app) {
});
}
app.handle(req, res, (err) => {
if (err != null && err instanceof Error) {
res
.status(500)
.send(`${reloadClientTag}<pre>${entities.escape(err.stack)}</pre>`);
} else {
next(err);
}
});
// Ensure that we never intercept budo-served resources. Otherwise, the Express app might have something like a 404 handler or "always require login" middleware that stops requests for budo assets from ever coming out the other end of the Express app.
if (req.url !== "/budo/livereload.js" && req.url !== `/${fullBundlePath}`) {
app.handle(req, res, (err) => {
if (err != null && err instanceof Error) {
res
.status(500)
.send(`${reloadClientTag}<pre>${entities.escape(err.stack)}</pre>`);
} else {
next(err);
}
});
} else {
next();
}
};
}
module.exports = function ({ options, staticPath, staticBasePath, entryPaths, host }) {
let middlewareList;
let fullBundlePath = (options.staticPrefix != null)
? path.join(options.staticPrefix, options.bundlePath)
: options.bundlePath;
if (options.middleware != null) {
middlewareList = assureArray(options.middleware).concat([
createExpressMiddleware(options.expressApp)
createExpressMiddleware(options.expressApp, fullBundlePath)
]);
} else {
middlewareList = [
createExpressMiddleware(options.expressApp)
createExpressMiddleware(options.expressApp, fullBundlePath)
];
}
@ -100,9 +109,7 @@ module.exports = function ({ options, staticPath, staticBasePath, entryPaths, ho
port: options.port,
host: host,
dir: staticBasePath,
serve: (options.staticPrefix != null)
? path.join(options.staticPrefix, options.bundlePath)
: options.bundlePath,
serve: fullBundlePath,
browserify: options.browserify,
middleware: middlewareList,
debug: defaultValue(options.sourceMaps, true),

Loading…
Cancel
Save