-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (55 loc) · 1.75 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require('dotenv').config()
const express = require('express')
const cors = require('cors')
const app = express()
const bodyParser = require('body-parser')
const dns = require('dns')
// Basic Configuration
const port = process.env.PORT || 3000
const shortUrlArray = []
console.log('**** Hello URL Shortener! ****')
app.use(cors())
app.use('/public', express.static(`${process.cwd()}/public`))
/* BODY PARSER */
app.use('/', bodyParser.urlencoded({ extended: false }))
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' })
})
// handle GET requests to /api/shorturl/:shorturl
app.get('/api/shorturl/:shorturl', function (req, res) {
const index = req.params.shorturl
// use 'index' to find original url in map
const redirectUrl = shortUrlArray[index]
// redirect to original url
res.redirect(redirectUrl)
})
// handle POST request to /api/shorturl
app.post('/api/shorturl', function (req, res) {
let parsedUrl = ''
const originalUrl = req.body.url
// See if URL is parsable
if (URL.canParse(originalUrl)) {
// If it can be parsed,
// - parse it
parsedUrl = new URL(originalUrl)
// then check if it is a valid url
dns.lookup(parsedUrl.hostname, (error, address, family) => {
if (error) {
// if it breaks
res.json({ error: 'invalid url' })
} else {
// proceed with adding the url to the global array
shortUrlArray.push(originalUrl)
// send back json
res.json({ original_url: originalUrl, short_url: shortUrlArray.lastIndexOf(originalUrl) })
}
})
}
})
app.listen(port, function () {
console.log(`Listening on port ${port}`)
})