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
The express.static middleware is separate from res.sendFile, You need to use an absolute path directly with res.sendFile. you can do it:
make a public directory in current project folder and put you html file there.
include path module in the server.js file like
const path = require('path');
res.sendFile(path.join(__dirname, './public', 'Apr29Home.html'));
Note: __dirname returns the directory that the currently executing script is in.