Message from JavaScript discussions

October 2020

— Exactly

— 

(req, res) => {
const methods = {
GET: () => cleanupForGet(req, res) && handleGet(req.url, res),
POST: () => {
const items = postParser(req)
poster(req, ...items)
res.end('OK')
}
}
if(methods[req.method]) return methods[req.method]()
res.setStatus(404)
res.end('404 Not Found')
}

— And the bug is fixed

— Can you find the bug?

— Res.end called two times

— Yep

— In the POST case

— If you need to combine switch with break or return anyway, you might as well go for single if statements or function maps

Message permanent page

— Wait, what?

— Where is the second time

— return res.end('OK') could solve it, right?

— Oh, yeah, missed that one ;)