11

I am very new to node and heroku and I suspect this is some kind of simple permission issue etc, but I can't seem to track it down.

I have several pure javascript files in a sub-directory one level beneath my root directory where my web.js file is sitting. I have a line in my web.js file to specify the directory

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

If I run my app locally with Heroku Foreman I get the expected js response when I run the following curl command

 curl localhost:5000/heatcanvas/heatcanvas.js

However when I push to Heroku and hit the corresponsing live url in the browser

www.example.com/heatcanvas/heatcanvas.js

I receive the following:

 Cannot GET /heatcanvas/heatcanvas.js

If I check Firebug and/or the Heroku logs I see I am actually getting 404 errors for those files even though the pathing should match what is being done locally. It is also worth mentioning that third party javascript is coming over just fine, it is only when the src attribute of the script tag points to my site that there is an issue. What do I need to do to get my scripts to be available?

kirps
  • 1,345
  • 2
  • 17
  • 28
  • 2
    I've never looked into the specifics, but I know that in Rails on Heroku, you put/compile your static assets into root/public. For instance Rails compiles `/assets/base.sass` to `/public/stylesheets/base.css`. So I think Heroku exposes the `public` dir as a static accessible dir. You generally can't access arbitrary files on production servers like you may be used to in conventional PHP apps. You'd have to specify a GET route that actually serves up that heatcanvas.js file -- but you wouldn't want to do that. I'm really rusty, but perhaps try to utilize the `public` dir. – danneu May 07 '12 at 00:13
  • 1
    On the [Cedar stack](https://devcenter.heroku.com/articles/cedar) requests are routed to the dyno un-disturbed. So it is up to the app/framework to serve static assets. The only special about the `public` directory is that the Rails app/framework serves assets from there. Node.js apps will have different structure. – Ryan Daigle Jun 21 '12 at 00:47
  • Can you please include the output of `heroku logs -t` during one of these requests? – Ryan Daigle Jun 21 '12 at 00:47
  • Is your `app.use()` in an `app.configure()` block that's not running on Heroku? You do have to specifically run `$ heroku config:add NODE_ENV=*` for Express to know what environment to use. – swider Jun 30 '12 at 22:38

1 Answers1

7

I recommend you to use process.cwd() value to get specific directory

process.env.PWD = process.cwd()

at the very beginning of your web.js

let you access files easily.

You can do

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

instead of using

__dirname

Warning: Make sure to execute web.js at the root directory of web.js (Heroku web.js are executed that way)

jwchang
  • 10,584
  • 15
  • 58
  • 89
  • 1
    Thanks, this helped me so much! Not sure if the OP has the same setup, but I'm running Heroku with several dynos, and it turns out you can't use `app.use(... __dirname+'/path');`, like I did locally. You have to use `process.env.PWD` instead. – Romain Oct 30 '12 at 23:26
  • As it seems, this does not work for me! Heroku always pre-appends a folder named "app", so it thinks my server.js (in root) is in an folder named "app". Testing locally is ok. – Pille Oct 06 '16 at 15:32
  • iam in the same status Pille how did you correct this app default folder events in this scope(local is always ok for me too) – Hamit YILDIRIM Mar 23 '17 at 13:41