Skip to content

Commit b5dd568

Browse files
fix: respect the bypass option with target/router options for proxy
1 parent 11d99c0 commit b5dd568

File tree

2 files changed

+31
-64
lines changed

2 files changed

+31
-64
lines changed

lib/Server.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -879,15 +879,17 @@ class Server {
879879
const { createProxyMiddleware } = require("http-proxy-middleware");
880880

881881
const getProxyMiddleware = (proxyConfig) => {
882-
const context = proxyConfig.context || proxyConfig.path;
883-
884-
// It is possible to use the `bypass` method without a `target`.
882+
// It is possible to use the `bypass` method without a `target` or `router`.
885883
// However, the proxy middleware has no use in this case, and will fail to instantiate.
886-
if (context) {
884+
if (proxyConfig.target) {
885+
const context = proxyConfig.context || proxyConfig.path;
886+
887887
return createProxyMiddleware(context, proxyConfig);
888888
}
889889

890-
return createProxyMiddleware(proxyConfig);
890+
if (proxyConfig.router) {
891+
return createProxyMiddleware(proxyConfig);
892+
}
891893
};
892894
/**
893895
* Assume a proxy configuration specified as:
@@ -913,9 +915,7 @@ class Server {
913915
? proxyConfigOrCallback()
914916
: proxyConfigOrCallback;
915917

916-
if (!proxyConfig.bypass) {
917-
proxyMiddleware = getProxyMiddleware(proxyConfig);
918-
}
918+
proxyMiddleware = getProxyMiddleware(proxyConfig);
919919

920920
if (proxyConfig.ws) {
921921
this.webSocketProxies.push(proxyMiddleware);

test/server/proxy-option.test.js

+23-56
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ const proxyOptionPathsAsProperties = {
4949
}
5050
},
5151
},
52+
"/bypass-with-target": {
53+
target: `http://localhost:${port1}`,
54+
changeOrigin: true,
55+
secure: false,
56+
bypass(req) {
57+
if (/\.(html)$/i.test(req.url)) {
58+
return req.url;
59+
}
60+
},
61+
},
5262
};
5363

5464
const proxyOption = {
@@ -223,6 +233,19 @@ describe("proxy option", () => {
223233
expect(response.status).toEqual(200);
224234
expect(response.text).toContain("proxy async response");
225235
});
236+
237+
it("should work with the 'target' option", async () => {
238+
const response = await req.get("/bypass-with-target/foo.js");
239+
240+
expect(response.status).toEqual(404);
241+
});
242+
243+
it("should work with the 'target' option #2", async () => {
244+
const response = await req.get("/bypass-with-target/index.html");
245+
246+
expect(response.status).toEqual(200);
247+
expect(response.text).toContain("Hello");
248+
});
226249
});
227250
});
228251

@@ -235,10 +258,6 @@ describe("proxy option", () => {
235258

236259
server = new Server(
237260
{
238-
static: {
239-
directory: staticDirectory,
240-
watch: false,
241-
},
242261
proxy: proxyOption,
243262
port: port3,
244263
},
@@ -274,10 +293,6 @@ describe("proxy option", () => {
274293

275294
server = new Server(
276295
{
277-
static: {
278-
directory: staticDirectory,
279-
watch: false,
280-
},
281296
proxy: proxyWithString,
282297
port: port3,
283298
},
@@ -313,10 +328,6 @@ describe("proxy option", () => {
313328

314329
server = new Server(
315330
{
316-
static: {
317-
directory: staticDirectory,
318-
watch: false,
319-
},
320331
proxy: proxyWithPath,
321332
port: port3,
322333
},
@@ -352,10 +363,6 @@ describe("proxy option", () => {
352363

353364
server = new Server(
354365
{
355-
static: {
356-
directory: staticDirectory,
357-
watch: false,
358-
},
359366
proxy: proxyWithRouterAsObject,
360367
port: port3,
361368
},
@@ -391,10 +398,6 @@ describe("proxy option", () => {
391398

392399
server = new Server(
393400
{
394-
static: {
395-
directory: staticDirectory,
396-
watch: false,
397-
},
398401
proxy: proxyOptionOfArray,
399402
port: port3,
400403
},
@@ -444,10 +447,6 @@ describe("proxy option", () => {
444447

445448
server = new Server(
446449
{
447-
static: {
448-
directory: staticDirectory,
449-
watch: false,
450-
},
451450
proxy: proxyOptionOfArrayWithoutTarget,
452451
port: port3,
453452
},
@@ -488,10 +487,6 @@ describe("proxy option", () => {
488487

489488
server = new Server(
490489
{
491-
static: {
492-
directory: staticDirectory,
493-
watch: false,
494-
},
495490
proxy: {
496491
"/proxy1": proxyTarget,
497492
"/proxy2": proxyTarget,
@@ -553,10 +548,6 @@ describe("proxy option", () => {
553548

554549
server = new Server(
555550
{
556-
static: {
557-
directory: staticDirectory,
558-
watch: false,
559-
},
560551
webSocketServer: webSocketServerType,
561552
proxy: [
562553
{
@@ -623,10 +614,6 @@ describe("proxy option", () => {
623614

624615
server = new Server(
625616
{
626-
static: {
627-
directory: staticDirectory,
628-
watch: false,
629-
},
630617
proxy: {
631618
"**": proxyTarget,
632619
},
@@ -755,10 +742,6 @@ describe("proxy option", () => {
755742

756743
server = new Server(
757744
{
758-
static: {
759-
directory: staticDirectory,
760-
watch: false,
761-
},
762745
proxy: {
763746
"*": {
764747
context: () => true,
@@ -808,10 +791,6 @@ describe("proxy option", () => {
808791

809792
server = new Server(
810793
{
811-
static: {
812-
directory: staticDirectory,
813-
watch: false,
814-
},
815794
proxy: {
816795
"/my-path": {
817796
target: "http://unknown:1234",
@@ -863,10 +842,6 @@ describe("proxy option", () => {
863842

864843
server = new Server(
865844
{
866-
static: {
867-
directory: staticDirectory,
868-
watch: false,
869-
},
870845
proxy: {
871846
"/my-path": {
872847
target: "http://unknown:1234",
@@ -921,10 +896,6 @@ describe("proxy option", () => {
921896

922897
server = new Server(
923898
{
924-
static: {
925-
directory: staticDirectory,
926-
watch: false,
927-
},
928899
proxy: {
929900
"/my-path": {
930901
target: "http://unknown:1234",
@@ -978,10 +949,6 @@ describe("proxy option", () => {
978949

979950
server = new Server(
980951
{
981-
static: {
982-
directory: staticDirectory,
983-
watch: false,
984-
},
985952
proxy: {
986953
"/my-path": {
987954
target: "http://unknown:1234",

0 commit comments

Comments
 (0)