-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
59 lines (48 loc) · 1.58 KB
/
server.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
var express = require('express')
var mongodb = require('mongodb')
var MongoClient = mongodb.MongoClient
var shortid = require('shortid')
var url = require('url')
var app = express()
app.set('port', (process.env.PORT || 5000))
app.enable('trust proxy')
var mongoUrl = process.env.MONGO_URI
var db;
// Initialize connection once
MongoClient.connect(mongoUrl, function(err, database) {
if(err) throw err;
db = database;
// Start the application after the database connection is ready
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'))
})
});
app.get('/new/*', function(req, res) {
var originalUrl = req.originalUrl.substr(5)
var longUrl = url.parse(originalUrl)
if (longUrl.hostname && (longUrl.hostname.slice(-1) !== ".") && (longUrl.hostname.indexOf('.') !== -1))
db.collection('url-shortener').insertOne({
url: originalUrl,
_id: shortid.generate()
}, function(err, docs) {
if (err) throw err
var shortURL = req.protocol + '://' + req.get('host') + '/' + docs.ops[0]._id;
res.send({
original_url: originalUrl,
short_url: shortURL
})
})
else
res.send({error: 'Invalid URL'})
})
app.get('/:shortUrl', function(req, res) {
db.collection('url-shortener').findOne({_id: req.params.shortUrl}, function(err, doc) {
if (err) {
throw err
}
if (doc)
res.redirect(301, doc.url)
else
res.sendStatus(404)
})
})