-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
102 lines (88 loc) · 2.54 KB
/
Copy pathserver.js
File metadata and controls
102 lines (88 loc) · 2.54 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
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
const rn = require('random-number');
const url = require('url');
const dns = require('dns');
const dotenv = require('dotenv').config();
const app = express();
const port = process.env.PORT;
const mongoDB = process.env.MONGODB_URI;
app.use(cors({ optionSuccessStatus: 200 })); // some legacy browsers choke on 204
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/public', express.static(path.join(__dirname, '/public')));
// Database connection setup
mongoose.connect(mongoDB, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
});
const db = mongoose.connection;
db.on('error', (err) => { console.log('Mongo DB connection error', err); });
db.once('open', () => { console.log('Mongo DB connected.'); });
// Schema setup
const Schema = mongoose.Schema;
const urlSchema = new Schema({
url: String,
short: Number,
}, {
timestamps: true,
});
const UrlModel = mongoose.model('url', urlSchema);
function addhttp(url) {
let newUrl = url;
if (!/^(?:f|ht)tps?\:\/\//.test(url)) {
newUrl = 'http://'.concat(url);
}
return newUrl;
}
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/views/index.html'));
});
app.get('/api/shorturl/:id', (req, res) => {
if (isNaN(req.params.id)) {
res.redirect('/');
} else {
const shortUrl = Number(req.params.id);
UrlModel.findOne({ short: shortUrl }, (err, data) => {
if (err) console.log(err);
else if (data) {
res.redirect(addhttp(data.url));
} else {
res.redirect('/');
}
});
}
});
app.post('/api/shorturl/new', (req, res) => {
const originalUrl = req.body.url;
const parsedUrl = url.parse(addhttp(originalUrl));
dns.lookup(parsedUrl.host, (err, address) => {
if (address === undefined) {
res.json({ error: 'invalid URL' });
} else {
const gen = rn.generator({
min: 1000,
max: 99999,
integer: true,
});
const shortUrl = gen();
const newUrl = new UrlModel({
url: originalUrl,
short: shortUrl,
});
newUrl.save((errSave) => {
if (errSave) console.log(errSave);
else {
res.json({ original_url: originalUrl, short_url: shortUrl });
}
});
}
});
});
const listener = app.listen(port, () => {
console.log('Your app is listening on port '.concat(listener.address().port));
});