-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: support optional rewrites #10
Comments
I'm not clear what you are asking me to do.. Maybe you could demonstrate what you are looking for with a simple middleware example. Please have a play with creating a simple middleware module: https://github.com/lwsjs/local-web-server/wiki/Creating-middleware. |
This is it: const Rewrite = require('lws-rewrite');
const path = require('path');
const fs = require('fs');
class ConditionalRewrite {
constructor() {
this.rewrite = new Rewrite();
}
optionDefinitions() {
return this.rewrite.optionDefinitions();
}
middleware(options, lws) {
const middlewares = this.rewrite.middleware(options, lws);
const directory = path.resolve(options.directory);
return middlewares.map((middleware) => {
return async (ctx, next) => {
const { url } = ctx.request;
const fullPath = path.join(directory, url);
try {
const stats = await fs.promises.stat(fullPath);
if (stats.isFile()) {
return next();
}
} catch {
//
}
return middleware(ctx, next);
};
});
}
}
module.exports = ConditionalRewrite; So basically, if a file is at the requested static location serve that and do not rewrite the request to something else. |
I see, thanks.. This would introduce an I/O performance cost since it touches the filesystem for every incoming request.. this cost would be noticeable for users building an SPA (which involves a lot of URLs which do not map to the filesystem, e.g. It's not something I would consider adding to the default stack (unless there's genuine demand for it) but feel free to publish and share this middleware plugin so others can use it, if required. |
I would need something like optiona/conditional rewrites. That could be similar as the spa conditionals
--spa.asset-test-fs
by checking if a file is available in the fs, then serve it, otherwise rewrite the request.The text was updated successfully, but these errors were encountered: