Ein Projekt das es ermöglicht Beerpong über das Internet von zwei unabhängigen positionen aus zu spielen. Entstehung im Rahmen einer Praktikumsaufgabe im Fach Interaktion.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

json.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*!
  2. * body-parser
  3. * Copyright(c) 2014 Jonathan Ong
  4. * Copyright(c) 2014 Douglas Christopher Wilson
  5. * MIT Licensed
  6. */
  7. /**
  8. * Module dependencies.
  9. */
  10. var bytes = require('bytes')
  11. var read = require('../read')
  12. var typer = require('media-typer')
  13. var typeis = require('type-is')
  14. /**
  15. * Module exports.
  16. */
  17. module.exports = json
  18. /**
  19. * RegExp to match the first non-space in a string.
  20. */
  21. var firstcharRegExp = /^\s*(.)/
  22. /**
  23. * Create a middleware to parse JSON bodies.
  24. *
  25. * @param {object} [options]
  26. * @return {function}
  27. * @api public
  28. */
  29. function json(options) {
  30. options = options || {}
  31. var limit = typeof options.limit !== 'number'
  32. ? bytes(options.limit || '100kb')
  33. : options.limit
  34. var inflate = options.inflate !== false
  35. var reviver = options.reviver
  36. var strict = options.strict !== false
  37. var type = options.type || 'json'
  38. var verify = options.verify || false
  39. if (verify !== false && typeof verify !== 'function') {
  40. throw new TypeError('option verify must be function')
  41. }
  42. function parse(body) {
  43. if (body.length === 0) {
  44. // special-case empty json body, as it's a common client-side mistake
  45. // TODO: maybe make this configurable or part of "strict" option
  46. return {}
  47. }
  48. if (strict) {
  49. var first = firstchar(body)
  50. if (first !== '{' && first !== '[') {
  51. throw new Error('invalid json')
  52. }
  53. }
  54. return JSON.parse(body, reviver)
  55. }
  56. return function jsonParser(req, res, next) {
  57. if (req._body) return next()
  58. req.body = req.body || {}
  59. if (!typeis(req, type)) return next()
  60. // RFC 7159 sec 8.1
  61. var charset = typer.parse(req).parameters.charset || 'utf-8'
  62. if (charset.substr(0, 4).toLowerCase() !== 'utf-') {
  63. var err = new Error('unsupported charset')
  64. err.status = 415
  65. next(err)
  66. return
  67. }
  68. // read
  69. read(req, res, next, parse, {
  70. encoding: charset,
  71. inflate: inflate,
  72. limit: limit,
  73. verify: verify
  74. })
  75. }
  76. }
  77. /**
  78. * Get the first non-whitespace character in a string.
  79. *
  80. * @param {string} str
  81. * @return {function}
  82. * @api public
  83. */
  84. function firstchar(str) {
  85. var match = firstcharRegExp.exec(str)
  86. return match ? match[1] : ''
  87. }