Slide 25
Slide 25 text
Example: HTTP Server
const server = http.createServer((req, res) => {
const query = url.parse(request.url, true).query
if (!query.uid) {
// do not track client errors
res.writeHead(400, { 'Content-Type': 'text/plain' })
return res.end('Bad Request')
}
db.get(`user:${query.uid}`, (err, user) => {
if (err) {
return trackError(err, () => {
res.writeHead(500, { 'Content-Type': 'text/plain' })
res.end('Internal Server Error')
})
}
if (!user) {
res.writeHead(404, { 'Content-Type': 'text/plain' })
return res.end(’Not Found')
}
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end(`${user.firstname} ${user.lastname}`)
})
})
server.listen(8080, (err) => {
if (err) throw err // re-throw this and handle it in the global handler
console.info('Server is listening on *:8080')
})
// basic global error handler
process.on('uncaughtException', (err) => {
trackError(err, () => process.exit(1))
})