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

master
Sven Slootweg 3 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) { return function expressMiddleware(req, res, next) {
try { try {
monkeyPatchEnd(res); monkeyPatchEnd(res);
@ -67,28 +67,37 @@ function createExpressMiddleware(app) {
}); });
} }
app.handle(req, res, (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 (err != null && err instanceof Error) { if (req.url !== "/budo/livereload.js" && req.url !== `/${fullBundlePath}`) {
res app.handle(req, res, (err) => {
.status(500) if (err != null && err instanceof Error) {
.send(`${reloadClientTag}<pre>${entities.escape(err.stack)}</pre>`); res
} else { .status(500)
next(err); .send(`${reloadClientTag}<pre>${entities.escape(err.stack)}</pre>`);
} } else {
}); next(err);
}
});
} else {
next();
}
}; };
} }
module.exports = function ({ options, staticPath, staticBasePath, entryPaths, host }) { module.exports = function ({ options, staticPath, staticBasePath, entryPaths, host }) {
let middlewareList; let middlewareList;
let fullBundlePath = (options.staticPrefix != null)
? path.join(options.staticPrefix, options.bundlePath)
: options.bundlePath;
if (options.middleware != null) { if (options.middleware != null) {
middlewareList = assureArray(options.middleware).concat([ middlewareList = assureArray(options.middleware).concat([
createExpressMiddleware(options.expressApp) createExpressMiddleware(options.expressApp, fullBundlePath)
]); ]);
} else { } else {
middlewareList = [ middlewareList = [
createExpressMiddleware(options.expressApp) createExpressMiddleware(options.expressApp, fullBundlePath)
]; ];
} }
@ -100,9 +109,7 @@ module.exports = function ({ options, staticPath, staticBasePath, entryPaths, ho
port: options.port, port: options.port,
host: host, host: host,
dir: staticBasePath, dir: staticBasePath,
serve: (options.staticPrefix != null) serve: fullBundlePath,
? path.join(options.staticPrefix, options.bundlePath)
: options.bundlePath,
browserify: options.browserify, browserify: options.browserify,
middleware: middlewareList, middleware: middlewareList,
debug: defaultValue(options.sourceMaps, true), debug: defaultValue(options.sourceMaps, true),

Loading…
Cancel
Save