Skip to content

Instantly share code, notes, and snippets.

@59023g
Last active February 26, 2017 02:18
Show Gist options
  • Save 59023g/247dc94f26d98244013125fe9c351ea8 to your computer and use it in GitHub Desktop.
Save 59023g/247dc94f26d98244013125fe9c351ea8 to your computer and use it in GitHub Desktop.
node http response.end() status code wrapper
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