Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http-proxy-middleware - access to the static files

I'm trying to combine my static landing page with the express.js app (react.js single page app).

On my landing page, I set up proxy using http-proxy-middleware My server.js for the static page looks like that:

var express = require('express');
var app = express();
var path = require('path');
var proxy = require('http-proxy-middleware');

var options = {
        target: 'http://localhost:9000', // the app
        changeOrigin: true
    };
var exampleProxy = proxy(options);

app.use('/app', exampleProxy);
app.use(express.static(__dirname + '/'))

app.listen(8080);

The problem is that when I get to localhost:8080/app/ I have access to correct index.html, but I cannot fetch the resources, the bundle.js is being fetched like that: http://localhost:8080/bundle.js, but apparently it is available on localhost:9000, not 8080

How do I make it so as the app has access to its static files?

like image 688
lipenco Avatar asked Jul 31 '26 09:07

lipenco


1 Answers

You can use the wildcard path matching of http-proxy-middleware module. You can implement the app.use without passing the path parameter.

app.use(proxy('/*.js', exampleProxy));
app.use(proxy('/api', exampleProxy));

This will serve your ".js" file through port 9000. Similarly, you can serve other assets as well by using this wildcard matching.

like image 197
amanpurohit Avatar answered Aug 03 '26 00:08

amanpurohit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!