GithubHelp home page GithubHelp logo

Comments (3)

arloduff avatar arloduff commented on May 18, 2024 15

Actually, I would also like this feature for my own project. Being able to serve local files for certain routes in a development environment is important for most Javascript projects.

from http-proxy-middleware.

chimurai avatar chimurai commented on May 18, 2024

imho, this would create unnecessary bloat in the proxy middleware for a specific use-case.

Think it is better to start a separate server to serve your static .json files, configure the proxy target and point it to this server.

This way you can customise your setup to your needs.

from http-proxy-middleware.

adamduncan avatar adamduncan commented on May 18, 2024

This issue's been closed for a while, but I was in need of a similar requirement in a create-react-app (proxy docs). Hope this helps someone.

To workaround, it was a case of storing mock JSON files in the site's public root directory (e.g. public/data/mocks/test.json – which get served from http://localhost:3000/data/mocks/... when running the application locally).

Then we configure rewrite on paths with a bogus /api prefix (which won't exist as routes in our application), and proxy to the original public files, removing that prefix and appending the file extension:

// setupProxy.js
const proxy = require('http-proxy-middleware');

const rewriteFn = function(path) {
  return path.split('/api')[1] + '.json';
};

module.exports = function(app) {
  app.use(
    proxy('/api/data/mocks', {
      target: 'http://localhost:3000', // same target app runs on
      pathRewrite: rewriteFn
    })
  );
};

With this setup, we should be able to access the mock data at both the original URL via filename (http://localhost:3000/data/mocks/test.json) and the mock API endpoint (http://localhost:3000/api/data/mocks/test).

Related: #333

from http-proxy-middleware.

Related Issues (20)

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.