diff --git a/docs/advanced-features/custom-server.md b/docs/advanced-features/custom-server.md index d887c841025c1..d7cb7bb6b2343 100644 --- a/docs/advanced-features/custom-server.md +++ b/docs/advanced-features/custom-server.md @@ -36,18 +36,24 @@ const app = next({ dev, hostname, port }) const handle = app.getRequestHandler() app.prepare().then(() => { - createServer((req, res) => { - // Be sure to pass `true` as the second argument to `url.parse`. - // This tells it to parse the query portion of the URL. - const parsedUrl = parse(req.url, true) - const { pathname, query } = parsedUrl - - if (pathname === '/a') { - app.render(req, res, '/a', query) - } else if (pathname === '/b') { - app.render(req, res, '/b', query) - } else { - handle(req, res, parsedUrl) + createServer(async (req, res) => { + try { + // Be sure to pass `true` as the second argument to `url.parse`. + // This tells it to parse the query portion of the URL. + const parsedUrl = parse(req.url, true) + const { pathname, query } = parsedUrl + + if (pathname === '/a') { + await app.render(req, res, '/a', query) + } else if (pathname === '/b') { + await app.render(req, res, '/b', query) + } else { + await handle(req, res, parsedUrl) + } + } catch (err) { + console.error('Error occurred handling', req.url, err) + res.statusCode = 500 + res.end('internal server error') } }).listen(port, (err) => { if (err) throw err