-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
33 lines (22 loc) · 804 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
const http = require('http')
const PORT = process.env.PORT || 3000
const server = http.createServer((req, res) => {
if (req.url === '/') return respondHello(req, res)
if (req.url === '/user-agent') return respondUserAgent(req, res)
if (req.url.match(/^\/b64\//)) return respondBase64(req, res)
res.end()
})
function respondHello (req, res) {
res.end(JSON.stringify({ msg: 'hello' }))
}
function respondUserAgent (req, res) {
const ua = req.headers['user-agent']
res.end(JSON.stringify({ ua }))
}
function respondBase64 (req, res) {
const phrase = req.url.replace(/^\/b64\//, '')
res.end(JSON.stringify({ b64: Buffer.from(phrase).toString('base64') }))
}
server.listen(PORT)
console.log(`Server listening on port ${PORT}`)
if (require.main !== module) module.exports = server