Developer
π§ Heroku Deployment¶
Heroku expects a Node.js app to have a server.js (or similar) and a package.json. Heroku wonβt serve plain HTML/CSS/JS without a minimal server process like express.
β Requirements¶
- A Heroku account
- Heroku CLI (installed in Gitpod)
- Git (already in Gitpod)
- A static
sitedirectory
π Project Structure¶
Assume you have a structure like:
/static-site
β
βββ public/
β βββ site
β βββ index.html
β βββ style.css
β
βββ server.js
βββ package.json
βββ .gitignore
π§© Step-by-Step Guide¶
1. Create a minimal Express server¶
Create server.js:
const express = require("express");
const path = require("path");
const app = express();
const PORT = process.env.PORT || 3000;
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, "public/site/")));
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
2. Create package.json¶
If not already created:
Then install express:
Update the scripts section in package.json:
3. Add a .gitignore file¶
4. Push to Heroku¶
Heroku will install dependencies, detect it's a Node.js app, and run the server.
π§ Why This Works¶
- Heroku requires a
startscript inpackage.json. - Heroku serves dynamic apps, so for static files, you need a server (e.g.,
express) to serveindex.htmland assets. public/site/is your static site root.