I searched through all topics, but could not find a similar issue.
I have a server.js
in the folder /build
// /build/server.js
import express from 'express';
const app = express();
app.use('/static', express.static(path.join(__dirname, 'build', 'static'), { maxAge: '30d' }));
and a css file in /build/static/css/my.css
. I start nodemon with build/server.js
from the root folder /
I assume that I should get my file via localhost:8080/static/css/my.css But it returns a 404 and "Cannot GET /static/css/my.css"
What could be the issue? I tried a lot of different paths, but never got a successful response.
Thanks in advance.
I finally decided to go with __dirname
to prevent errors caused by relative paths to the directory the server is started from.
The main problem was that __dirname
always returned /
. I added the following lines to my webpack config:
target: 'node', // was already there
node: { // has been added
__filename: false,
__dirname: false
},
You can find a closer description of this behaviour of __dirname in webpack here: https://github.com/webpack/webpack/issues/2010#issuecomment-181256611
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.