-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
122 lines (109 loc) · 3.24 KB
/
Copy pathserver.js
File metadata and controls
122 lines (109 loc) · 3.24 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict'
var express = require('express')
var sanitize = require('mongo-sanitize') // security
var helmet = require('helmet')
var app = express()
app.use(helmet())
app.use(helmet.hidePoweredBy())
var path = require('path')
var bodyParser = require('body-parser')
var mongoose = require('mongoose')
var config = require('./config')
var base58 = require('./base58.js')
var validator = require('validator')
var server = require('http').Server(app)
// grab the url model
var Url = require('./models/url')
mongoose.connect('mongodb://' + config.db.host + '/' + config.db.name, function (err) {
if (err) throw err
})
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static(path.join(__dirname, 'public')))
// static landing page
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'views/index.html'))
})
// handle shorten POST
app.post('/api/shorten', function (req, res) {
var longUrl = sanitize(req.body.url)
var shortUrl = ''
if (!validator.isURL(longUrl)) {
console.log('Not a valid Url')
return res.status(400).send({error: 'not a valid url'})
}
// check if url already exists in database
Url.findOne({long_url: longUrl}, function (err, doc) {
if (err) {
return res.status(500).send({ error: 'Something failed!' })
}
if (doc) {
shortUrl = config.webhost + base58.encode(doc._id)
// the document exists, so we return it without creating a new entry
res.send({
'shortUrl': shortUrl,
'longUrl': longUrl
})
} else {
// since it doesn't exist, let's go ahead and create it:
var newUrl = Url({
long_url: longUrl
})
// save the new link
newUrl.save(function (err) {
if (err) {
console.log(err)
return res.status(500).send({ error: 'Something failed!' })
}
shortUrl = config.webhost + base58.encode(newUrl._id)
res.send({
'shortUrl': shortUrl,
'longUrl': longUrl
})
})
}
})
})
app.get('/api/urls', function (req, res) {
Url.find({}, function (err, docs) {
if (err) {
return res.status(500).end({ error: 'Something failed!' })
}
for (var i = 0; i < docs.length; i++) {
var doc = {
_id: docs[i]['_id'],
long_url: docs[i]['long_url'],
hits: docs[i]['hits']
}
var id = doc['_id']
doc.shortUrl = config.webhost + base58.encode(id)
docs[i] = doc
}
return res.send({urls: docs})
})
})
// get long URL from shortened one
app.get('/:encoded_id', function (req, res) {
var base58Id = req.params.encoded_id
base58.decodeAsync(base58Id, function (err, id) {
if (err) {
return res.redirect(config.webhost)
}
// check if url already exists in database
Url.findOneAndUpdate({_id: id}, {$inc: {hits: 1}}, {new: true}, function (err, doc) {
if (err) {
return res.status(500).send({ error: 'Something failed!' })
}
if (doc) {
return res.redirect(doc.long_url)
} else {
return res.redirect(config.webhost)
}
})
})
})
var port = process.env.PORT || process.argv[2] || 8000
server.listen(port, function () {
console.log(`Server listening on port ${port}`)
})
module.exports = server