-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (48 loc) · 1.54 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
const express = require('express');
const ejs = require('ejs');
const bodyParser = require('body-parser');
const fs = require('fs');
const { traceDeprecation } = require('process');
const app = express();
let domain = 'http://localhost:8080';
let port = 8080;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser());
app.get('/', (req, res) => {
res.render('home.ejs')
})
app.get('/url/:url', (req, res) => {
let getUrls = fs.readFileSync('urls.json', 'utf-8');
getUrls = JSON.parse(getUrls);
for (let i = 0; i < getUrls.length; i++) {
let reqId = req.params.url;
reqId = parseInt(reqId);
if (getUrls[i].id == reqId) {
res.redirect(getUrls[i].destination)
break;
}
}
})
app.get('/shortened/:linkId', (req, res) => {
res.render('shortened.ejs', {
shortenedLink: `${domain}/url/${req.params.linkId}`
})
})
app.post('/shorten', (req, res) => {
let url = req.body.urlToShorten;
let linkId = Math.floor(Math.random() * 1000000)
let getUrls = fs.readFileSync('urls.json', 'utf-8');
getUrls = JSON.parse(getUrls);
let urlData = encodeURIComponent({linkId: linkId})
getUrls.push({id: linkId, destination: url})
fs.writeFileSync('urls.json', JSON.stringify(getUrls))
res.redirect(`/shortened/${linkId}`)
})
app.listen(port, (err) => {
if (err) {
console.log(err);
} else {
console.log(`Listening to port: ${port}`);
}
})