summaryrefslogtreecommitdiff
path: root/lib/utils.js
blob: 061070e36750acb4a7be112a044c849f41184ed5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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); };