Search code examples
node.jsherokudeploymentwebdeploy

How to Fix Not Found Error with Heroku Deployment


I am new to web-development and am having some issues deploying my very basic website with Heroku. Upon creating my app, I have successfully pushed my app to git and deployed it to Heroku. Also, it does run locally just fine.

However, upon opening the app (using "Heroku open" command and clicking the "Open app" button on the Heroku app page itself), I am getting a page that says Not Found.

I have been searching for a solution all over and would really appreciate some help here. Thank you.

Procfile:

web: node server.js

package.json:

 {                                                                               
   "name": "LHSite",                                                           
   "version": "1.0.0",                                                         
   "engines": {                                                                
       "node": "13.12.x"                                                       
   },                                                                          
   "scripts": {                                                                
       "start": "node server.js"                                               
   },                                                                          
   "dependencies": {                                                           
      "express": "4.17.1"                                                     
   },                                                                          
   "author": "Hunter Estrada",                                                 
   "license": "ISC"
 }

server.js:

var express = require('express');
var app = express();
var port = process.env.PORT || 8080
app.use(express.static(__dirname));
app.get("/", function(req, res) {
    res.sendFile("/Users/<user>/CS/LHSite/Apr29Home.html");
}
app.listen(port, function() {
    console.log("app running");
}

File structure: file structure for my website

Here is the url for my app: https://lh-anni-2.herokuapp.com


Solution

  • The express.static middleware is separate from res.sendFile, You need to use an absolute path directly with res.sendFile. you can do it:

    1. make a public directory in current project folder and put you html file there.

    2. include path module in the server.js file like

    const path = require('path');

    1. replace your res.sendFile with below line of code

    res.sendFile(path.join(__dirname, './public', 'Apr29Home.html'));

    Note: __dirname returns the directory that the currently executing script is in.