Skip to content
Snippets Groups Projects
httputils.js 1.54 KiB
Newer Older
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// various little utilities to make crafting boilerplate responses
// simple

exports.fourOhFour = function(resp, reason)
{
  resp.writeHead(404, {"Content-Type": "text/plain"});
  resp.write("Not Found");
  if (reason) {
    resp.write(": " + reason);
  }
  resp.end();
};

exports.serverError = function(resp, reason)
{
  resp.writeHead(500, {"Content-Type": "text/plain"});
  if (reason) resp.write(reason);
  resp.end();
};

exports.badRequest = function(resp, reason)
{
  resp.writeHead(400, {"Content-Type": "text/plain"});
  resp.write("Bad Request");
  if (reason) {
    resp.write(": " + reason);
  }
  resp.end();
};

exports.forbidden = function(resp, reason)
{
  resp.writeHead(403, {"Content-Type": "text/plain"});
  resp.write("Forbidden");
  if (reason) {
    resp.write(": " + reason);
  }
  resp.end();
};

exports.throttled = function(resp, reason)
{
  resp.writeHead(429, {"Content-Type": "text/plain"});
  resp.write("Too Many Requests");
  if (reason) {
    resp.write(": " + reason);
  }
  resp.end();
};

exports.jsonResponse = function(resp, obj)
{
  resp.writeHead(200, {"Content-Type": "application/json"});
Lloyd Hilaiel's avatar
Lloyd Hilaiel committed
  if (obj !== undefined) resp.write(JSON.stringify(obj));
exports.xmlResponse = function(resp, doc)
{
  resp.writeHead(200, {"Content-Type": "text/xml"});
  if (doc !== undefined) resp.write(doc);
  resp.end();
};