Git Product home page Git Product logo

Comments (3)

75lb avatar 75lb commented on August 27, 2024

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.

KnisterPeter avatar KnisterPeter commented on August 27, 2024

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.

75lb avatar 75lb commented on August 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.