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.

index.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*!
  2. * morgan
  3. * Copyright(c) 2010 Sencha Inc.
  4. * Copyright(c) 2011 TJ Holowaychuk
  5. * Copyright(c) 2014 Jonathan Ong
  6. * Copyright(c) 2014 Douglas Christopher Wilson
  7. * MIT Licensed
  8. */
  9. /**
  10. * Module dependencies.
  11. */
  12. var auth = require('basic-auth')
  13. var deprecate = require('depd')('morgan')
  14. var onFinished = require('on-finished')
  15. /**
  16. * Default log buffer duration.
  17. */
  18. var defaultBufferDuration = 1000;
  19. /**
  20. * Create a logger middleware.
  21. *
  22. * @param {String|Function} format
  23. * @param {Object} [options]
  24. * @return {Function} middleware
  25. * @api public
  26. */
  27. exports = module.exports = function morgan(format, options) {
  28. if (typeof format === 'object') {
  29. options = format
  30. format = options.format || 'default'
  31. // smart deprecation message
  32. deprecate('morgan(options): use morgan(' + (typeof format === 'string' ? JSON.stringify(format) : 'format') + ', options) instead')
  33. }
  34. if (format === undefined) {
  35. deprecate('undefined format: specify a format')
  36. }
  37. options = options || {}
  38. // output on request instead of response
  39. var immediate = options.immediate;
  40. // check if log entry should be skipped
  41. var skip = options.skip || function () { return false; };
  42. // format function
  43. var fmt = compile(exports[format] || format || exports.default)
  44. // options
  45. var stream = options.stream || process.stdout
  46. , buffer = options.buffer;
  47. // buffering support
  48. if (buffer) {
  49. var realStream = stream
  50. var buf = []
  51. var timer = null
  52. var interval = 'number' == typeof buffer
  53. ? buffer
  54. : defaultBufferDuration
  55. // flush function
  56. var flush = function(){
  57. timer = null
  58. if (buf.length) {
  59. realStream.write(buf.join(''));
  60. buf.length = 0;
  61. }
  62. }
  63. // swap the stream
  64. stream = {
  65. write: function(str){
  66. if (timer === null) {
  67. timer = setTimeout(flush, interval)
  68. }
  69. buf.push(str);
  70. }
  71. };
  72. }
  73. return function logger(req, res, next) {
  74. req._startAt = process.hrtime();
  75. req._startTime = new Date;
  76. req._remoteAddress = getip(req);
  77. function logRequest(){
  78. if (skip(req, res)) return;
  79. var line = fmt(exports, req, res);
  80. if (null == line) return;
  81. stream.write(line + '\n');
  82. };
  83. // immediate
  84. if (immediate) {
  85. logRequest();
  86. } else {
  87. onFinished(res, logRequest)
  88. }
  89. next();
  90. };
  91. };
  92. /**
  93. * Compile `format` into a function.
  94. *
  95. * @param {Function|String} format
  96. * @return {Function}
  97. * @api private
  98. */
  99. function compile(format) {
  100. if (typeof format === 'function') {
  101. // already compiled
  102. return format
  103. }
  104. if (typeof format !== 'string') {
  105. throw new TypeError('argument format must be a function or string')
  106. }
  107. var fmt = format.replace(/"/g, '\\"')
  108. var js = ' return "' + fmt.replace(/:([-\w]{2,})(?:\[([^\]]+)\])?/g, function(_, name, arg){
  109. return '"\n + (tokens["' + name + '"](req, res, "' + arg + '") || "-") + "';
  110. }) + '";'
  111. return new Function('tokens, req, res', js);
  112. };
  113. /**
  114. * Define a token function with the given `name`,
  115. * and callback `fn(req, res)`.
  116. *
  117. * @param {String} name
  118. * @param {Function} fn
  119. * @return {Object} exports for chaining
  120. * @api public
  121. */
  122. exports.token = function(name, fn) {
  123. exports[name] = fn;
  124. return this;
  125. };
  126. /**
  127. * Define a `fmt` with the given `name`.
  128. *
  129. * @param {String} name
  130. * @param {String|Function} fmt
  131. * @return {Object} exports for chaining
  132. * @api public
  133. */
  134. exports.format = function(name, fmt){
  135. exports[name] = fmt;
  136. return this;
  137. };
  138. /**
  139. * Apache combined log format.
  140. */
  141. exports.format('combined', ':remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"')
  142. /**
  143. * Apache common log format.
  144. */
  145. exports.format('common', ':remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length]')
  146. /**
  147. * Default format.
  148. */
  149. exports.format('default', ':remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"');
  150. deprecate.property(exports, 'default', 'default format: use combined format')
  151. /**
  152. * Short format.
  153. */
  154. exports.format('short', ':remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms');
  155. /**
  156. * Tiny format.
  157. */
  158. exports.format('tiny', ':method :url :status :res[content-length] - :response-time ms');
  159. /**
  160. * dev (colored)
  161. */
  162. exports.format('dev', function(tokens, req, res){
  163. var color = 32; // green
  164. var status = res.statusCode;
  165. if (status >= 500) color = 31; // red
  166. else if (status >= 400) color = 33; // yellow
  167. else if (status >= 300) color = 36; // cyan
  168. var fn = compile('\x1b[0m:method :url \x1b[' + color + 'm:status \x1b[0m:response-time ms - :res[content-length]\x1b[0m');
  169. return fn(tokens, req, res);
  170. });
  171. /**
  172. * request url
  173. */
  174. exports.token('url', function(req){
  175. return req.originalUrl || req.url;
  176. });
  177. /**
  178. * request method
  179. */
  180. exports.token('method', function(req){
  181. return req.method;
  182. });
  183. /**
  184. * response time in milliseconds
  185. */
  186. exports.token('response-time', function(req, res){
  187. if (!res._header || !req._startAt) return '';
  188. var diff = process.hrtime(req._startAt);
  189. var ms = diff[0] * 1e3 + diff[1] * 1e-6;
  190. return ms.toFixed(3);
  191. });
  192. /**
  193. * UTC date
  194. */
  195. exports.token('date', function(){
  196. return new Date().toUTCString();
  197. });
  198. /**
  199. * response status code
  200. */
  201. exports.token('status', function(req, res){
  202. return res._header ? res.statusCode : null;
  203. });
  204. /**
  205. * normalized referrer
  206. */
  207. exports.token('referrer', function(req){
  208. return req.headers['referer'] || req.headers['referrer'];
  209. });
  210. /**
  211. * remote address
  212. */
  213. exports.token('remote-addr', getip);
  214. /**
  215. * remote user
  216. */
  217. exports.token('remote-user', function (req) {
  218. var creds = auth(req)
  219. var user = (creds && creds.name) || '-'
  220. return user;
  221. })
  222. /**
  223. * HTTP version
  224. */
  225. exports.token('http-version', function(req){
  226. return req.httpVersionMajor + '.' + req.httpVersionMinor;
  227. });
  228. /**
  229. * UA string
  230. */
  231. exports.token('user-agent', function(req){
  232. return req.headers['user-agent'];
  233. });
  234. /**
  235. * request header
  236. */
  237. exports.token('req', function(req, res, field){
  238. return req.headers[field.toLowerCase()];
  239. });
  240. /**
  241. * response header
  242. */
  243. exports.token('res', function(req, res, field){
  244. return (res._headers || {})[field.toLowerCase()];
  245. });
  246. /**
  247. * Get request IP address.
  248. */
  249. function getip(req) {
  250. return req.ip
  251. || req._remoteAddress
  252. || (req.connection && req.connection.remoteAddress)
  253. || undefined;
  254. }