-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
70 lines (58 loc) · 1.86 KB
/
Copy pathapp.js
File metadata and controls
70 lines (58 loc) · 1.86 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require('express')
const mongoose = require('mongoose') // 載入 mongoose
const exphbs = require('express-handlebars');
const shortenURL = require('./shortenURL')
const bodyParser = require('body-parser')
const Link = require('./models/link')
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const app = express()
app.engine('hbs', exphbs({ defaultLayout: 'main', extname: '.hbs' }))
app.set('view engine', 'hbs')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static("public"))
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
// 取得資料庫連線狀態
const db = mongoose.connection
// 連線異常
db.on('error', () => {
console.log('mongodb error!')
})
// 連線成功
db.once('open', () => {
console.log('mongodb connected!')
})
// 設定首頁路由
app.get('/', (req, res) => {
res.render('index')
})
app.post('/', (req, res) => {
const originURL = req.body.originURL
const shortURL = shortenURL()
//若短網址存在,則重新generate一個
while(Link.findOne({ shortURL }) === 'null') {
shortURL = shortenURL()
}
if(!originURL){
//若使用者沒有輸入URL,就跳過
} else {
Link.findOne({ originURL }) //若使用者輸入相同網址,便回傳相同縮網址
.lean()
.then(link => {
return link ? link : Link.create({originURL, shortURL})
})
.then(link => res.render('result', { origin: req.headers.origin, short: link.shortURL }))
.catch(error => console.error(error))
}
})
app.get('/:shortCode', (req, res) => {
const shortURL = req.params.shortCode
Link.findOne({ shortURL })
.then(link => res.redirect(link.originURL))
.catch(error => console.error(error))
})
// 設定 port 3000
app.listen(3000, () => {
console.log('App is running on http://localhost:3000')
})