Heroku(Cedar) + Node + Express + Jade Client-side javascript files in subdirectory work locally with foreman+curl but not when pushed to Heroku

假装没事ソ 提交于 2019-12-09 16:17:53

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/10367600/herokucedar-node-express-jade-client-side-javascript-files-in-subdirecto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!