Last active
February 26, 2017 02:18
-
-
Save 59023g/247dc94f26d98244013125fe9c351ea8 to your computer and use it in GitHub Desktop.
node http response.end() status code wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function endResponse( res, code, err ) { | |
const enc = 'utf8' | |
switch ( code ) { | |
case 200: | |
res.writeHead( 200 ) | |
return await res.end( JSON.stringify( { | |
status: 200, | |
message: 'ok' | |
} ), enc ) | |
case 500: | |
res.writeHead( 500 ) | |
return await res.end( JSON.stringify( { | |
status: 500, | |
message: 'internal server error', | |
enc | |
} ), 'utf8' ) | |
case 405: | |
res.writeHead( 405 ) | |
return await res.end( JSON.stringify( { | |
status: 405, | |
message: 'invalid method' | |
} ), enc ) | |
break; | |
default: | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment