Deploying Nodejs on Heroku does not serve static files

I am trying to do two things that I am reading but not working. No statics files found.

This does not work:

process.env.PWD = process.cwd()
app.set('views', path.join(process.env.PWD, 'public'));

      

And it doesn't work:

enter code here

app.set ('views', path.join (__ dirname, 'public'));

+3


source to share


2 answers


Heroku doesn't work with __dirname for some reason, so try this:

process.env.PWD = process.cwd();

app.set('views', path.join(process.env.PWD, 'public'));

app.use(express.static(path.join(process.env.PWD, 'public')));

      



This is what works for me when I have this problem.

+7


source


This is because Views are partial parts that need to be inside the "views" folder, for example

app.set('views', path.join(__dirname, 'views'));



For static files (HTML, CSS, JS), they must be inside a shared folder, so for server files from a shared folder, you need

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

Thanks to

-1


source







All Articles