Comments (3)
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.
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.
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)
- Memory leak detected on the EventEmitter with http-proxy-middleware.js where server.on('close') listener is added for non websocket usage HOT 5
- POST request looses body HOT 2
- Unable to output console.log and my plugin is not running HOT 4
- v3 Next.js API Route Websocket multiple requests HOT 3
- Shadow/mirror requests HOT 2
- Express multipart/form-data multer files HOT 1
- Memory leak in function `responseInterceptor` of file `handlers/response-interceptor.ts` HOT 1
- Next.js App Router can't handle proxied requests HOT 16
- Filter example in README wrong. HOT 1
- Cannot override options in the `onProxyReq` method. HOT 1
- websocket messages interception & manipulation
- Intercept HTTP(s) calls and modify request "paths" on the fly HOT 1
- Issue obtaining body and updating it as part of proxy HOT 3
- Proxy request fails due to bodyParser HOT 1
- Fails to proxy from localhost to localhost HOT 2
- Too many subscriptions to Server.close event cause a OOM? HOT 4
- TS type compatibility HOT 4
- ERR_HTTP_HEADERS_SENT from onProxyReq with https-proxy-agent HOT 2
- Proxy to another proxy HOT 1
- Security Vulnerability in Dependency | follow-redirects HOT 2
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 http-proxy-middleware.