-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
99 lines (82 loc) · 2.68 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
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
'use strict';
require('dotenv').config();
const dns = require('dns');
const cors = require('cors');
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const EncodedURL = require('./models/EncodedURL');
const app = express();
// Basic Configuration
const port = process.env.PORT || 3000;
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/public', express.static(process.cwd() + '/public'));
// Setup MongoDB
mongoose.connect(process.env.MONGOLAB_URI, { useNewUrlParser: true });
// API endpoints...
app.get('/', function(req, res) {
res.sendFile(process.cwd() + '/views/index.html');
});
app.post('/api/shorturl/new', function(req, res) {
const urlRegex = /^(ftp|http|https):\/\/[^ "]+$/;
const originalURL = req.body.url;
if (!urlRegex.test(originalURL)) {
return res.send({ error: 'invalid URL' });
}
// parse URL to check if hostname is legit
const typedURL = new URL(originalURL);
// perform DNS lookup
dns.lookup(typedURL.hostname, (err) => {
if (err) {
console.log(err);
return res.send({ error: 'invalid URL' });
}
// check if link already exists
EncodedURL.findOne({ original_url: originalURL }, (err, data) => {
if (err) {
return console.log(err);
}
if (data) {
return res.send({
original_url: data.original_url,
short_url: data.short_url
});
}
// if it doesnt exist
// create links
const shortURL = Math.floor(Math.random() * 1000);
const links = new EncodedURL({
original_url: originalURL,
short_url: shortURL
});
// save links to database
links.save((err, data) => {
if (err) {
return console.log(err);
}
app.get(`/api/shorturl/${data.short_url}`, (req, res) => {
res.redirect(data.original_url);
});
return res.send({
original_url: data.original_url,
short_url: data.short_url
});
});
});
});
});
EncodedURL.find((err, docs) => {
if (err) {
return console.log(err);
}
for (let i = 0; i < docs.length; i++) {
app.get(`/api/shorturl/${docs[i].short_url}`, (req, res) => {
res.redirect(docs[i].original_url);
});
}
});
// start server
app.listen(port, function() {
console.log('Node.js listening at port 3000...');
});