2

Using Express to serve a Vue.js webpack app, I am receiving the following error after deploy: enter image description here Is my code for serving the app is the issue here?

app.use(helmet())

app.use(express.static(path.resolve(__dirname, '../client/dist/static')));

app.all('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, '../client/dist', 'index.html'));
})

Otherwise isn't express.static supposed to automatically assign content types to the static files?

softcode
  • 4,358
  • 12
  • 41
  • 68
  • similar issue but my problem was overlooking that **express.static()** needs an absolute path. – Minsky Feb 19 '22 at 14:07

3 Answers3

6

You get this message also when the response status in 404 Not Found, so check carefully that the files actually exist from Network tab.

Network tab:

status 404

Console tab:

enter image description here

See a similar issue here: https://stackoverflow.com/a/48792698/258772

mrts
  • 16,697
  • 8
  • 89
  • 72
2

For some reason, had to now specify a mount path:

app.use('/static', express.static(path.resolve(__dirname, '../client/dist/static')));
softcode
  • 4,358
  • 12
  • 41
  • 68
  • 1
    I had the same issue. Clearing `npm cache` did not help, nor did reinstalling all `node_modules` - but giving it a mount path. Thanks a lot for sharing! – Martin Bories Mar 27 '18 at 22:43
  • What if I want to host the static files at the root of the app :( – Josh Mc Dec 08 '18 at 10:32
0

Yes same problems over here.

I'm using ecstatic (still with express) now. I'm not sure if this is the solution (I'm not on the machine that made the error possible).

Will try it tommorow on the 'error' machine.

const express = require('express');
const ecstatic = require('ecstatic');
const http = require('http');

const app = express();

app.use(ecstatic({
  root: `${__dirname}/public`,
  showdir: false,
}));

http.createServer(app).listen(8080);

console.log('See if its cool on -> :8080');
Johan Hoeksma
  • 3,534
  • 5
  • 28
  • 40