-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (52 loc) · 1.45 KB
/
app.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
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
const path = require('path')
const express = require('express')
const app = express()
const utils = require('./utils')
const persistance = require('./persistance')
const PORT = process.env.PORT || 8080
const DOMAIN = process.env.domain || 'http://127.0.0.1:8080'
app.use(express.json())
app.use(express.static('static'))
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, '/public/index.html'))
})
app.post('/shorten', [function (req, res, next) {
if ('url' in req.body) {
next()
} else {
res.status(400).json({'status': 'failure', 'reason': 'malformed request'})
}
}, function (req, res) {
let body = req.body
let shortCode = utils.generateShortCode()
let shortUrl = utils.createShortUrl(DOMAIN, shortCode)
persistance.saveUrl(shortCode, body.url, function(){
let result = {
'status': 'success',
'short_url': shortUrl
}
res.json(result)
})
}])
app.get('/:shortcode', function (req, res, next){
let shortCode = req.params.shortcode
persistance.getUrl(shortCode, function (err, reply){
if (err){
throw err
} else if (reply == null){
next()
} else {
res.redirect(reply.toString())
}
})
})
app.use(function (req, res, next) {
res.status(404).send("Sorry can't find that!")
})
app.use(function (err, req, res, next) {
console.error(err.stack)
res.status(500).send('Something broke!')
})
app.listen(PORT, function () {
console.log('Shorty running on port : ', PORT)
})