0

ok, I have the simple server which is the following setup:

var port = process.env.PORT || 8080;

var server = http.createServer(app).listen(port);

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

app.use(express.static(process.env.PWD+'/Public'));

app.get('/',function(req,res) {

  res.sendfile(process.env.PWD+ '/index.html');
  res.end;

});

I create a git in the folder and I successfully push it to heroku, but when I open it I get this result:

https://leoforio.herokuapp.com/

If you open the console log you will see that it fails to load css and js static files which can't be found,although when I test my app locally this setup works and the files are served as planned.

What am I doing wrong?

P.S. I don't think it a problem with heroku,I believe the problem is within nodejs and uploading the app.

drizo
  • 267
  • 6
  • 14

1 Answers1

2

I'm going to guess that the problem lies in how you're specifying the directory for static files.

What I normally see -- and prefer -- is setting up static this way:

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

That means Express will look for the static files in the public subdirectory of the same directory that your application file is in. Let's imagine you have two apps, app1.js and app2.js. app1.js is configured the way you have it, using process.cwd() (current working directory), and app2.js is using __dirname as its base. Both of these apps are in /home/drizo/app.

If you do this, both will work correctly:

cd /home/drizo/app
node app1.js              # uses cwd + /public = /home/drizo/app/public
node app2.js              # uses __dirname + /public = /home/drizo/app/public

However, if you do it like this:

cd /home/drizo           # or ANY directory that's not /home/drizo/app
node app/app1.js             # uses cwd + /public = /home/drizo/public - WRONG
node app/app2.js             # uses __dirname + /public = /home/drizo/app/public

As you can see, app1.js is using the directory that the app was started from. I'm not sure how Heroku starts/restarts apps, but I'm guessing it isn't doing it from the directory you expect.

Ethan Brown
  • 26,892
  • 4
  • 80
  • 92
  • Hey ethan,thanks for answerng!I used your suggestion before the one I use now and it didn't work either.Let me try again and I will tell you! – drizo Dec 06 '14 at 20:54
  • It seems to me that heroku or whoever executes the app.js file fails to 'understand' the 'app.use(express.static(__dirname + '/public'));' because if you go to console it says that it can;t find the file in the same folder as app.js. – drizo Dec 06 '14 at 20:58
  • also consider that the app when it is in my local machine it works fine, when I upload to heroku that is when everything fails! – drizo Dec 06 '14 at 20:59
  • Why don't you try logging `__dirname` to the console, and maybe this: `console.log(__dirname); console.log(require('fs').existsSync(__dirname + '/public'));`...if you have access to the console log, it might give you some clues.... – Ethan Brown Dec 06 '14 at 21:13
  • Make sure the `public` subdirectory exists (I see you have it as `Public` with a capital P...non-Windows servers are case sensitive....) – Ethan Brown Dec 06 '14 at 21:14
  • 1
    that is the problem when you are a self-taught programmer like me, you dont know the theoritical stuff. Thanks Ethan sensitivity was the problem for me. check the ling it is up and running! – drizo Dec 06 '14 at 21:40