Skip to content

Commit

Permalink
support path rewrites
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancahill committed Sep 23, 2019
1 parent 8c5636f commit 8cd6851
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serve-handler-proxied",
"version": "5.0.7-2",
"version": "5.0.7-canary.5",
"description": "The routing foundation of `serve` and static deployments on Now",
"main": "src/index.js",
"scripts": {
Expand Down
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const {promisify} = require('util');
const path = require('path');
const {stat, createReadStream, readdir} = require('fs');
const {URL} = require('url');

// Packages
const url = require('fast-url-parser');
Expand Down Expand Up @@ -176,16 +177,19 @@ const shouldProxy = (decodedPath, {proxy = []}, onError) => {
const matches = sourceMatches(source, decodedPath, true);

if (matches) {
const u = new URL(destination);

const server = httpProxy.createProxyServer({
changeOrigin: true,
target: destination,
target: u.origin,
...options
});

server.on('error', onError);

return {
server
server,
target: toTarget(source, u.pathname, decodedPath)
};
}
}
Expand Down Expand Up @@ -592,6 +596,8 @@ module.exports = async (request, response, config = {}, methods = {}) => {

if (proxy) {
try {
const o = url.parse(request.url);
request.url = o.search ? `${proxy.target}${o.search}` : proxy.target;
proxy.server.web(request, response);
} catch (err) {
return internalError(absolutePath, response, acceptsJSON, current, handlers, config, err);
Expand Down

0 comments on commit 8cd6851

Please sign in to comment.