This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (53 loc) · 1.93 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
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const querystring = require('querystring');
require('dotenv').config()
// Custom shortened URL model
const ShortUrl = require('./models/shortUrl');
// Server Port and Mongo Database URL
const PORT = process.env.PORT
const DB_URL = process.env.MONGO_DB_URL
// Open connection to the MongoDB
mongoose.connect(DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
});
// Set ViewEngine and Querystring library
app.set('view engine', 'ejs');
app.use(express.urlencoded({extended: false}));
// Make asset files available
app.use(express.static(__dirname + '/public/assets'));
// Index page
app.get('/', async (request, response) => {
const shortUrls = await ShortUrl.find();
response.render('index', { shortUrls: shortUrls, shortenedUrl: request.query.shortenedUrl});
});
// URL Shortening process
app.post('/shorten', async (request, response) => {
const fullUrl = (request.query.fullUrl == null ? request.body.fullUrl : request.query.fullUrl);
var shortenedUrl = await ShortUrl.findOne({ full: fullUrl });
if (shortenedUrl == null)
await ShortUrl.create({ full: fullUrl });
shortenedUrl = await ShortUrl.findOne({ full: fullUrl });
const query = querystring.stringify({
"shortenedUrl": shortenedUrl.short
});
if (request.accepts('html'))
response.redirect('/?' + query);
else
response.json({ "shortenedURL" : shortenedUrl.short });
});
// Redirection engine
app.get('/:shortUrl', async (request, response) => {
const shortUrl = await ShortUrl.findOne({ short: request.params.shortUrl });
if (shortUrl == null)
return response.sendStatus(404);
shortUrl.clicks++;
shortUrl.save();
response.redirect(shortUrl.full)
});
// Start listening
var server = app.listen(PORT, function () {
console.log("URL Shortener server listening at: " + PORT)
});