dvg
dvg

Reputation: 181

How to make express serve static files from another upper directory?

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

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

app.use(express.static();

look like?

----UPDATE---

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

Upvotes: 6

Views: 7881

Answers (3)

dvg
dvg

Reputation: 181

Actually solved my problem by using:

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

Upvotes: 9

patelarpan
patelarpan

Reputation: 7991

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

Upvotes: 1

AmirHossein Rd
AmirHossein Rd

Reputation: 255

You can use path.join()

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

Upvotes: 1

Related Questions