-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
47 lines (40 loc) · 1.05 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
const express=require('express');
const app=express();
const cryptoRandomString = require('crypto-random-string');
app.use(express.urlencoded({extended:false}))
const urls=[
{id:1,full:"abc.com/xyz/post/123",short:"first"}
];
app.get('/', (req, res) => {
res.sendFile(__dirname+"/index.html")
})
app.get('/urls', (req, res) => {
res.send(urls)
})
app.get('/urls/:id', (req, res) => {
const data=urls.find(d => d.short==req.params.id)
data ? res.redirect(`https://${data.full}`): res.status(404).send("no data found")
})
app.post('/urls', function (req, res) {
let url=req.body.inputurl
let new_url=""
if(url.match("https://"))
{
new_url=url.replace("https://", "");
}
else if(url.match("http://"))
{
new_url=url.replace("http://", "");
}
else{
new_url=url;
}
urls.push({
id:urls.length+1,
full: new_url,
short:cryptoRandomString({length: 5, type: 'alphanumeric'})
});
console.table(urls)
res.redirect('/')
})
app.listen(process.env.PORT||3000);