0

In my app there are some static files I would like to serve to the client, but the server does not respond.

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

This is the relevant piece of code I can't fix alone.

2 Answers 2

1

The problem lies in the comma - , between __dirname and /public.

You should change to that:

app.use(express.static(__dirname + '/public')); 

Uni_Nake suggested using prefix before public folder

app.use('/public', express.static(__dirname + '/public')); 
Sign up to request clarification or add additional context in comments.

Comments

0

looks like the use of app.use and express.static are being messed up. The code you have will serve the whole of the current directory under / via express, which i doubt is what you want.

the following will make everything within /public available under /public :

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

i.e. if you have /public/banner.gif you'll be able to access it through:

http://localhost:{port}/pubic/banner.gif

or you could use the following to and omit the /public from the http request:

app.use(express.static(__dirname + '/public'));

i.e. if you have /public/banner.gif you'll be able to access it through:

http://localhost:{port}/banner.gif

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.