Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

DRY up the simple handlers.

  • Loading branch information...
commit 2dc2c4d7a4d6d5681c244ec6be3b7f3bb3cf4751 1 parent 4411c0e
Tim Caswell authored
Showing with 8 additions and 15 deletions.
  1. +8 −15 lib/node-router.js
23 lib/node-router.js
View
@@ -150,32 +150,25 @@ exports.getServer = function getServer(logger) {
doRoute();
}
- res.simpleText = function (code, body, extra_headers) {
+ function simpleResponse(code, body, content_type, extra_headers) {
res.writeHead(code, (extra_headers || []).concat(
- [ ["Content-Type", "text/plain"],
+ [ ["Content-Type", content_type],
["Content-Length", body.length]
]));
res.write(body);
res.close();
+ }
+
+ res.simpleText = function (code, body, extra_headers) {
+ simpleResponse(code, body, "text/plain", extra_headers);
};
res.simpleHtml = function (code, body, extra_headers) {
- res.writeHead(code, (extra_headers || []).concat(
- [ ["Content-Type", "text/html"],
- ["Content-Length", body.length]
- ]));
- res.write(body);
- res.close();
+ simpleResponse(code, body, "text/html", extra_headers);
};
res.simpleJson = function (code, json, extra_headers) {
- var body = JSON.stringify(json);
- res.writeHead(code, (extra_headers || []).concat(
- [ ["Content-Type", "application/json"],
- ["Content-Length", body.length]
- ]));
- res.write(body);
- res.close();
+ simpleResponse(code, JSON.stringify(json), "application/json", extra_headers);
};
res.notFound = function (message) {
Please sign in to comment.
Something went wrong with that request. Please try again.