-
Notifications
You must be signed in to change notification settings - Fork 98
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
Implement proxy config option #75
base: master
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## master #75 +/- ##
==========================================
- Coverage 99.42% 95.38% -4.04%
==========================================
Files 2 2
Lines 345 368 +23
==========================================
+ Hits 343 351 +8
- Misses 2 17 +15
Continue to review full report at Codecov.
|
@leo Friendly bump to this. If there's not interest I'd be interested in maintaining a fork of the package to support this feature. |
Bump, I'd love to see this move forward as well and would be willing to help. |
+1 imho this is essential. i'm going to have to switch to another library due to lack of proxy support, which is too bad because serve is otherwise great. |
@gatherben Meanwhile you can use my fork with this feature if you'd like. Is published as |
Why this PR has not been merged yet? It's a great and useful feature. |
@nathancahill OK, I think you should add related tests and documentation as the description of PR template. |
@JounQin I'm not investing more time until I hear back from the maintainers that they are interested (in which case this PR can be finished) or not interested (in which case the feature will be available in serve-proxied on npm). |
@nathancahill I tried your // serve.json
{
"proxy": [
{
"source": "/api/",
"destination": "http://localhost:8080/",
"secure": false,
"changeOrigin": true
}
]
} CMD: // response
{"error":{"code":"not_found","message":"The requested path could not be found"}} Is there anything wrong? |
@JounQin make sure the service running on |
@nathancahill Yeah, of course, it works with webpack-dev-server so I believe the service is running correctly. Whatever, I've finished up by writing a custom const path = require('path');
const http = require('http');
const httpProxy = require('http-proxy');
const handler = require('serve-handler');
const proxy = httpProxy.createProxyServer();
const PROXIES = require('../.proxyrc.json');
const PROXY_SOURCES = Object.keys(PROXIES);
const server = http.createServer((request, response) => {
const proxySource = PROXY_SOURCES.find(source => request.url.match(source));
if (proxySource) {
const proxyConfig = PROXIES[proxySource];
return proxy.web(request, response, proxyConfig, error => {
response.destroy(error);
console.dir({
source: request.url,
target: proxyConfig.target,
message: error.message,
});
});
}
return handler(request, response, {
public: path.resolve('dist/static'),
rewrites: [
{
source: '/**',
destination: '/index.html',
},
],
});
});
server.listen(4200, () => console.log('Running at http://localhost:4200')); |
Added support for routing segments like in the {
"proxy": [
{ "source": "/api/:object", "destination": "http://example.com/:object" }
]
} Published on npm as serve-proxied@11.1.0 |
@leo Did you guys ever have any time to review these pending PRs? |
use http-proxy: const handler = require("serve-handler");
const http = require("http");
const path = require("path");
const proxy = require("http-proxy").createProxyServer();
const apiAddr = "http://192.168.1.2:31501/"; // proxy host
const server = http.createServer((request, response) => {
if (request.url.indexOf("/es") > -1) { // request api
console.log("proxying req url: " + request.url);
return proxy.web(request, response, { target: apiAddr }, (err) => {
console.error(err);
});
}
// You pass two more arguments for config and middleware
// More details here: https://github.com/vercel/serve-handler#options
return handler(request, response, {
public: path.resolve("./dist/es-web-portal"),
});
});
server.listen(3000, () => {
console.log("Running at http://localhost:3000");
}); |
Hello, I have used your fork but I need to list all possible path as follow |
Uses well-established http-proxy to provide an option similar to
rewrite
that proxies requests to a different backend. Resolves #30. The new option would look like this:Additionally, other http-proxy options can be specified in the config, like
ignorePath
which removes the original url when the request is proxied:Wanted to put this in a PR to get it in front of the Zeit team before going further. If the idea is good, I'll finish the remaining todos:
proxy
config optionproxy
option in @zeit/schema repo