1. 程式人生 > >nodejs漸入佳境[16]-node express專案部署到heroku

nodejs漸入佳境[16]-node express專案部署到heroku

原始檔

views/partials/footer.hbs:

1
2
3
<Header>
   <footer>{{pageTitle}}</footer>
<Header>

views/about.hbs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
 <head>
   <meta charset="utf-8">
   <title
>
Some Website</title>

 </head>
 <body>
   <h1>{{pageTitle}}</h1>
   <p><a href="/">Home</a></p>
   <p><a href="/about">About</a></p>
   <p>Some text here</p>

   {{> footer}}

 </body>
</html>
sx

express.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const express = require('express');
const hbs = require('hbs');
const fs = require('fs');
var app = express();

const port = process.env.PORT || 3000;

hbs.registerPartials(__dirname + '/views/partials'
);

app.set('view engine','hbs');
// 引數是一個middleware
app.use(express.static(__dirname +'/public'));
//返回html格式
app.get('/',(req,res)=>{
 res.send('<h1>Hello world</h1>');
});

//返回json格式
app.get('/fast',(req,res)=>{
 res.send('<h1>Hello world</h1>');
});

//返回檔案,about.hbs在views資料夾下
app.get('/about',(req,res)=>{
 res.render('about.hbs',{
   pageTitle:'About Page',
   currentYear:new Date().getFullYear()
 });
});
//監聽埠,  第二個回撥是開啟伺服器後呼叫
app.listen(port,()=>{
 console.log('hello jonson');
});

git

1
2
3
4
.gitignore裡面的檔案不會提交
git init
git add .
git commit -m "fitst commit"

heroku

安裝heroku-cli 略…

1
2
3
4
heroku login  // 登陸賬號密碼
hexoru create //建立分支
git push heroku master //提交到heroku管理的遠端分支
hexoru open   /開啟網址

參考:

heroku部署
heroku監控臺

image.png