Guest User

Untitled

a guest
Oct 16th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. var ErrorPage = require("error-page"),
  2. StringDecoder = require("string_decoder").StringDecoder,
  3. Negotiator = require("negotiator"),
  4. Templar = require("templar"),
  5. querystring = require("querystring"),
  6. extend = require("xtend")
  7.  
  8. var isJSON = /\/(x-)?json$/
  9.  
  10. module.exports = Routil()
  11.  
  12. function Routil() {
  13. var config = {
  14. errorPage: {},
  15. templar: {}
  16. }
  17.  
  18. return {
  19. config: configure,
  20. Routil: Routil,
  21. ErrorPage: ErrorPage,
  22. errorPage: errorPage,
  23. redirect: redirect,
  24. mediaTypes: mediaTypes,
  25. sendJson: sendJson,
  26. sendHtml: sendHtml,
  27. send: send,
  28. methods: methods,
  29. template: template,
  30. Templar: Templar,
  31. body: body,
  32. formBody: formBody,
  33. jsonBody: jsonBody
  34. }
  35.  
  36. function errorPage(req, res, details) {
  37. if (Array.isArray(details)) {
  38. var page = ErrorPage(req, res, config.errorPage)
  39. page.apply(page, details)
  40. } else {
  41. ErrorPage(req, res, config.errorPage)(details)
  42. }
  43. }
  44.  
  45. function redirect(req, res, target, statusCode) {
  46. res.statusCode = statusCode || 302
  47. res.setHeader('location', target)
  48.  
  49. mediaTypes(req, res, {
  50. "application/json": function () {
  51. sendJson(res, {
  52. redirect: target,
  53. statusCode: statusCode
  54. })
  55. },
  56. "default": function () {
  57. var html = '<html><body><h1>Moved'
  58. if (statusCode === 302) {
  59. html += ' Permanently'
  60. }
  61. html += '</h1><a href="' + target + '">' + target + '</a>'
  62.  
  63. sendHtml(res, html)
  64. }
  65. })()
  66. }
  67.  
  68. function mediaTypes(req, res, object) {
  69. var types = Object.keys(object),
  70. mediaType = new Negotiator(req).preferredMediaType(types)
  71.  
  72. return object[mediaType] || object.default || notSupportedHandler
  73.  
  74. function notSupportedHandler() {
  75. errorPage(req, res,
  76. [new Error("mediaType not supported"), 415])
  77. }
  78. }
  79.  
  80. function sendJson(res, object, statusCode) {
  81. send(res, JSON.stringify(object), statusCode, {
  82. "Content-Type": "application/json"
  83. })
  84. }
  85.  
  86. function sendHtml(res, data, statusCode) {
  87. send(res, data, statusCode, {
  88. "Content-Type": "text/html"
  89. })
  90. }
  91.  
  92. function send(res, data, statusCode, headers) {
  93. if (!Buffer.isBuffer(data)) {
  94. data = new Buffer(data)
  95. }
  96.  
  97. res.writeHead(statusCode || res.statusCode || 200,
  98. extend((headers || {}), {
  99. "Content-Length": data.length
  100. }))
  101.  
  102. res.end(data)
  103. }
  104.  
  105. function methods(routes) {
  106. return requestHandler
  107.  
  108. function requestHandler(req, res, params) {
  109. var method = req.method
  110.  
  111. if (routes[method]) {
  112. return routes[method](req, res, params)
  113. }
  114. errorPage(req, res, 405)
  115. }
  116. }
  117.  
  118. function configure(object) {
  119. extend(config, object)
  120. }
  121.  
  122. function template(req, res, name, data) {
  123. Templar(req, res, config.templar)(name, data)
  124. }
  125.  
  126. function body(req, callback) {
  127. if (req.body) {
  128. callback(req.body)
  129. }
  130.  
  131. var requestBody = "",
  132. stringDecoder = new StringDecoder
  133.  
  134. req.on("data", addToBody)
  135.  
  136. req.on("end", returnBody)
  137.  
  138. function addTobody(buffer) {
  139. requestBody += stringDecoder.write(buffer)
  140. }
  141.  
  142. function returnBody() {
  143. req.body = requestBody
  144. callback(requestBody)
  145. }
  146. }
  147.  
  148. function formBody(req, res, callback) {
  149. if (req.headers['Content-Type'] !==
  150. 'application/x-www-form-urlencoded'
  151. ) {
  152. // XXX Add support for formidable uploading, as well
  153. errorPage(req, res, 415)
  154. }
  155. body(req, parseBody)
  156.  
  157. function parseBody(body) {
  158. callback(querystring.parse(body))
  159. }
  160. }
  161.  
  162. function jsonBody(req, res, callback) {
  163. if (!req.headers["Content-Type"].match(isJSON)) {
  164. return errorPage(req, res, 415)
  165. }
  166. body(req, extractJSON)
  167.  
  168. function extractJSON(body) {
  169. try {
  170. var json = JSON.parse(body)
  171. } catch (error) {
  172. return errorPage(req, res, [400, error])
  173. }
  174. callback(json)
  175. }
  176. }
  177. }
Add Comment
Please, Sign In to add comment