Comments (3)
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.
from rewrite.
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.
from rewrite.
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. /user/3
etc) since this middleware would try to find /user/3
and every other URL on the filesystem before responding..
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.
from rewrite.
Related Issues (10)
- Feature request: Cookie domain rewrite HOT 4
- Error: Cannot find module 'req-then' HOT 3
- rewriting to remote targets via a corporate HTTP proxy is currently down HOT 1
- Feature request: support proxying websocket connections HOT 1
- `https-proxy-agent` v2.x includes high severity vulnerability HOT 2
- Wrong handling of paths without params HOT 4
- Rewrite to a path outside of rootDir HOT 3
- Consider making rewrite domain-aware HOT 10
- Removal of "secure" breaks "SameSite=None" HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rewrite.