Hello Node.js on Heroku
作業の流れ
GitHubリポジトリ作成
git clone https://github.com/ユーザ名/アプリ名.git
アプリ制作
サンプルコード
index.html
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>Node App on Heroku</title> </head> <body> <H1>Hello Heroku.</H1> </body> </html>
index.js
const express = require("express"); const app = express(); app.use(express.static("public")); app.get("/",function(req,res){ res.sendFile(__dirname+"/index.html"); }); app.listen(process.env.PORT || 5000, function(){ console.log("The server is runnning on port 5000."); });
Procfile
web: node index.js
package.json
{ "name": "herokuapp", "version": "1.0.0", "description": "heroku test app", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "engines": { "node": "12.x" }, "dependencies": { "express": "^4.15.2" }, "author": "", "license": "MIT" }
.gitignore
/node_modules npm-debug.log .DS_Store /*.env
node.jsアプリ初期化
nmp init npm install
ローカルテスト
heroku local web
Herokuアプリ作成
heroku login heroku create
Herokuアプリコミット
git add . git commit -m "heroku test"
Herokuへデプロイ
git push heroku master
Herokuアプリ起動
heroku open