-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
79 lines (70 loc) · 2.17 KB
/
Copy pathapp.js
File metadata and controls
79 lines (70 loc) · 2.17 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
71
72
73
74
75
76
77
78
79
const express = require('express')
const app = express()
const exphbs = require('express-handlebars')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const URL = require('./models/urls')
let longurl = ''
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
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.engine('hbs', exphbs({ defaultLayout: 'main', extname: '.hbs' }))
app.set('view engine', 'hbs')
app.use(bodyParser.urlencoded({ extended: true }))
app.get('/', (req, res) => {
return res.render('index')
})
app.post('/shorten', (req, res) => {
longurl = ''
longurl += req.body.url
if (!req.body.url) {
res.redirect('/nourl')
}
// 判斷是否已有相同的長網址被存入資料庫
else{
URL.findOne({ longurl }, (error, urls) => {
if (!urls) {
// 生成隨機的短代碼
const shortCode = Math.random().toString(36).substr(2, 5)
// 構建縮短網址
const shorturl = "http://localhost:3000/" + shortCode
return URL.create({ shortURL:shorturl, longURL:longurl })
.then(() => res.redirect('/show'))
.catch(error => console.log(error))
}
else {
res.redirect('/show')
}
})
}
})
app.get('/nourl',(req, res) => {
res.render('nourl')
})
app.get('/show', (req, res) => {
URL.find({ longURL: longurl })
.lean()
.then(url => res.render('show', { url: url[0] }))
.catch(error => console.log(error))
})
app.get('/:short', (req, res) => {
const Shorturl = 'http://localhost:3000' + req.url
console.log(Shorturl)
URL.find({ shortURL:Shorturl })
.lean()
.then(url => {
res.redirect(url[0].longURL)
})
.catch(error => console.log(error))
})
app.listen(3000, () => {
console.log(`App is running on http://localhost:3000`)
})