-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
35 lines (30 loc) · 928 Bytes
/
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
import http from 'http'
import { URL } from 'url'
import getBrowsers from './get-browsers.js'
const DEFAULT_QUERY = 'defaults'
const PORT = process.env.PORT || 5000
http
.createServer(async (req, res) => {
let url = new URL(req.url, `http://${req.headers.host}/`)
if (url.pathname === '/') {
let query = url.searchParams.get('q') || DEFAULT_QUERY
let queryWithoutQuotes = query.replace(/'/g, '')
try {
let browsers = await getBrowsers(queryWithoutQuotes)
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/json'
})
res.write(JSON.stringify(browsers))
res.end()
} catch (error) {
res.writeHead(400, {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/json'
})
res.write(JSON.stringify({ error }))
res.end()
}
}
})
.listen(PORT)