9

I have tried several ways of serving up a static directory. Here is a simple way I am doing this.

var app = express();

app.all('*', express.static('./public'));

module.exports = app;

// run the server
http.createServer(app).listen(8080);

I have several other configurations like:

app.use('/', express.static('./public'));

There is an index.html file in the public directory that gets served up fine. The only thing in the HTML file is a request for a JavaScript file. When that request gets made, express throws a 301 redirect, and adds a trailing slash.

Here is the HTML:

<script type="text/javascript" src="/dist/bundle.js"></script>

Here is the network request.

enter image description here

Any help is appreciated.

  • 1
    I had this problem because of some bug which I had fixed but it kept recurring because of the browser cache. Try disabling the browser cache before reloading. – adarsh Aug 20 '20 at 08:00

2 Answers2

6

You can enable strict route mode and use a route middleware (https://stackoverflow.com/a/15773824/781251)

http://expressjs.com/en/api.html

const router = express.Router({ strict: true })
Eduardo Stuart
  • 2,869
  • 18
  • 22
  • 3
    Would this only work with using the express.Router class or can this also be set as an option using the shorthand app.get() declaration? – Dennis Bauszus Jan 28 '20 at 10:58
1

This means the asset can't be found. It renders a 404 response.

Bram z
  • 735
  • 2
  • 7
  • 20