Heroku Cannot GET /

浪子不回头ぞ 提交于 2019-12-12 07:25:44

问题


I am new to Heroku and believe I am following all of the steps outlined on Heroku's website to deploy via node.js – https://devcenter.heroku.com/articles/getting-started-with-nodejs – but despite indications of success, I only see this in the browser when I go to my newly-generated herokuapp.com site.

Cannot GET /

No errors when executing

git push heroku master

My Procfile is simply

web: node app.js

I dont quite understand dynos yet, but there seems to be one running:

heroku ps === web (1X): node app.js web.1: up 2014/07/03 23:55:00 (~ 18m ago)

Then:

heroku open Opening APP-NAME... done

But https://APP-NAME.herokuapp.com/ just displays the Cannot GET / message.


回答1:


I had my dist directory included in my .gitignore file so I was not committing dist to my repo and not pushing it to Heroku. Therefore, Heroku could not find any content to serve.

I updated my .gitignore, committed, and pushed, and my app shows up just fine on Heroku now.




回答2:


almost 3 years, but I'm answering for reference.

  • generally /dist is a build process generated directory, it's temporary and changes a lot while working in our sources, so /dist is not versioned.
  • on your package.json you can add a script called postinstall with this build process, let's say you have a task in gulp called build...so "postinstall": "gulp build"
  • if you manage your project's dependencies in bower too, npm install --save bower and "postinstall": "./node_modules/bower/bin/bower install && gulp build"

Just a simple example croped for your package.json

{
   "dependencies: {
    "bower":"^1.8.0",
    "grunt":"^1.0.1",
   },
   "scripts": {
    "start": "node ./www.js",
    "build": "grunt dist",
    "postinstall": "./node_modules/bower/bin/bower install && npm run build"
   }
}

Obvious you're probably done and better nowadays...I'm just referencing it for next consultings.




回答3:


I would think you haven't added the files to git. Whatever file you've edited on your local machine, you need to git add xyz.ext, git commit -m "Message", git push heroku master -u (-u will save the 'heroku master' parameters so future additions you will only need to type git push). In short, every time you're asked to deploy the app, you need to git add, git commit, git push. Hope that helps.




回答4:


I dont know why it worked but i changed the location of my angular /dist from [root]client/dist to [root]/dist which is at the same directory level as server.js




回答5:


Adding these two lines in server.js worked for me:

var distDir = __dirname + "/dist/"; app.use(express.static(distDir));

My dist structure is as follows:




回答6:


Make sure you have these 2 things in place:

  1. In your package.json, include heroku-postbuild line, in my case is like this, (it might change if you need to do more than one install)

    "scripts": { 
    "heroku-postbuild": "npm install"
    

    },

  2. In your index.js file, add a condition for production

    if (process.env.NODE_ENV === "production") {

       app.use(express.static("build"));
    
    
       app.get("*", (req, res) => {
            res.sendFile(path.resolve(__dirname,  "build", "index.html"));
        });
    
      }
    

    Express will serve up production assets like our main.js file, or main.css file!

    Finally- Express look if the request is for an asset Express will serve up the index.html file if it doesn't recognize the route



来源:https://stackoverflow.com/questions/24566635/heroku-cannot-get

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