/
mock-server.js
34 lines (30 loc) · 955 Bytes
/
mock-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
const express = require('express');
const request = require('request');
const bodyParser = require('body-parser');
const app = express();
const port = 8000;
const SHORTURLKEY = process.env.SHORTURLKEY;
const SHORTURLAPI = "https://kutt.it/api/url/submit";
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
app.post('/.netlify/functions/shorturl', (req, res) => {
let longURL = req.body.longUrl;
request
.post({
url: SHORTURLAPI,
headers: {
'X-API-Key': SHORTURLKEY
},
json: true,
form: {
target: longURL,
reuse: true
}
}, (error, response, body) => {
res.json(JSON.stringify({
'shortURL': body.shortUrl
}));
});
});
app.listen(port, () => console.log(`HSDeckViewer listening on port ${port}`));