forked from rishz/URL-Shortener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (33 loc) · 950 Bytes
/
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
/**
* Created by rishabhshukla on 09/03/17.
*/
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const shortner = require("./shortner");
const port = 4100;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}))
app.use(express.static("static"));
app.get('/:shortcode',(req,res) => {
shortner.expand(req.params.shortcode)
.then((url) => {
res.redirect(url);
})
.catch((error) => {
});
});
app.post('/api/v1/shorten', (req, res) => {
let url = req.body.url;
let shortcode = shortner.shorten(url);
res.send(shortcode);
});
app.get('/api/v1/expand/:shortcode', (req, res) => {
let shortcode = req.body.shortcode;
let url = url.expand(shortcode);
res.send(url);
});
app.listen(port, () => {
console.log("Listening on port "+port);
});
//console.log(shortner.shorten('http://google.com'));