-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (35 loc) · 1.05 KB
/
index.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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const isUrl = require('is-url');
// Basic Configuration
const port = process.env.PORT || 3000;
const urlToShort = [];
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/public', express.static(`${process.cwd()}/public`));
app.get('/', function(req, res) {
res.sendFile(process.cwd() + '/views/index.html');
});
// Your first API endpoint
app.get('/api/hello', function(req, res) {
res.json({ greeting: 'hello API' });
});
app.get('/api/shorturl/:id', (req, res) => {
const {id} = req.params;
res.redirect( urlToShort[parseInt(id) - 1] );
});
app.post('/api/shorturl', (req, res) => {
const {url} = req.body
console.log(url)
if(!isUrl(url)){
res.json({ error: 'invalid url' });
}
urlToShort.push(url);
res.json({ original_url: url, short_url: urlToShort.length });
});
app.listen(port, function() {
console.log(`Listening on port ${port}`);
});