3
votes

If I have an nodejs express application with folowing folder structure:

-src - client - public - css - js - ... - views - server - server.js

  • How can I serve the static files in the public folder from the server.js file, since it is located above the index.js root location?
  • How should the:

app.use(express.static();

look like?

----UPDATE---

SOLVED this by using: app.use(express.static(path.join(__dirname, '/../client/public')));

3
path.resolve(__dirname, '../client/public/') might also work . e.g. with res.sendFile instead of express.static .Alex Glukhovtsev

3 Answers

5
votes

Actually solved my problem by using:

app.use(express.static(path.join(__dirname, '/../client/public')));

0
votes

You can use path.join()

app.use(express.static(path.join(__dirname,'public')));
0
votes

just do this, (as per your directory structure)

app.use(express.static(path.join(__dirname, 'src/client/public'))); 
// http://localhost:3000/hello.html

or

app.use('/static', express.static(path.join(__dirname, 'src/client/public')))
// http://localhost:3000/static/hello.html