-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
57 lines (50 loc) · 1.64 KB
/
Copy pathapp.js
File metadata and controls
57 lines (50 loc) · 1.64 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
const express = require('express')
const exphbs = require('express-handlebars');
const URL = require('./models/URL')
const bodyParser = require('body-parser')
const generateURL = require('./lib/generateURL')
const isValidURL = require('./lib/isValidURL')
require('./config/mongoose')
const app = express()
app.engine('hbs', exphbs({ defaultLayout: 'main', extname: '.hbs' }))
app.set('view engine', 'hbs')
app.use(bodyParser.urlencoded({ extended: true }))
//首頁路由
app.get('/',(req,res)=>{
res.render('index')
})
//接收原始網址
app.post('/url', (req, res) => {
const originalURL = req.body.originalURL;
if (isValidURL(originalURL)){
URL.findOne({ originalURL })
.then(existingURL => {
if (existingURL) {
return res.render('index', { originalURL, shortenedURL: existingURL.shortenedURL });
} else {
const shortenedURL = generateURL();
URL.create({ originalURL: originalURL, shortenedURL: shortenedURL })
.then(() => res.render('index', { originalURL, shortenedURL }))
}
})
.catch(error => console.log(error));
}else{
res.render('error', { originalURL })
}
})
//導回原始網站
app.get('/:shortenedURL',(req,res)=>{
const shortenedURL=req.params.shortenedURL
URL.findOne({ shortenedURL })
.then(existingURL => {
if (existingURL) {
res.redirect(existingURL.originalURL);
} else {
res.redirect('/'); // 如果找不到對應的短網址,可以導向首頁或其他處理方式
}
})
.catch(error => console.log(error));
})
app.listen(3000,()=>{
console.log('App is running on http://localhost:3000')
})