diff options
author | Alexis Hovorka <[email protected]> | 2022-02-02 00:06:53 -0700 |
---|---|---|
committer | Alexis Hovorka <[email protected]> | 2022-02-02 00:06:53 -0700 |
commit | 8a943500d97598a0b49ef655dc1b5484fe5e83d8 (patch) | |
tree | 339daf640084d724524c3cfcf92e51f48f7037b0 /lib/utils.js |
Initial commit
Diffstat (limited to 'lib/utils.js')
-rw-r--r-- | lib/utils.js | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..061070e --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,17 @@ +"use strict"; + +const ct = module.exports.ct = (res, mime, code) => + res.writeHead(code||200, {"Content-Type": mime||"application/json"}); + +module.exports.res204 = (res) => { res.statusCode = 204; res.end(); } +module.exports.err400 = (res, msg) => { ct(res, "text/plain", 400); res.end(""+(msg||"400 Bad Request")); } +module.exports.err401 = (res, msg) => { ct(res, "text/plain", 401); res.end(""+(msg||"401 Unauthorized")); } +module.exports.err403 = (res, msg) => { ct(res, "text/plain", 403); res.end(""+(msg||"403 Forbidden")); } +module.exports.err404 = (res, msg) => { ct(res, "text/plain", 404); res.end(""+(msg||"404 Not Found")); } +module.exports.err500 = (res, msg) => { ct(res, "text/plain", 500); res.end(""+(msg||"500 Internal Server Error")); } +module.exports.sj = (res, data, code) => { ct(res, null, code); res.end(JSON.stringify(data)); } + +module.exports.cors = fn => (req, res, ...rest) => { + res.setHeader("Access-Control-Allow-Origin", "*"); + res.setHeader("Access-Control-Allow-Methods", "GET, POST"); + fn(req, res, ...rest); }; |