Dieses Repository beinhaltet HTML- und Javascript Code zur einer NotizenWebApp auf Basis von Web Storage. Zudem sind Mocha/Chai Tests im Browser enthalten. https://meinenotizen.netlify.app/
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.

README.md 11KB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. # morgan
  2. [![NPM Version][npm-version-image]][npm-url]
  3. [![NPM Downloads][npm-downloads-image]][npm-url]
  4. [![Build Status][travis-image]][travis-url]
  5. [![Test Coverage][coveralls-image]][coveralls-url]
  6. HTTP request logger middleware for node.js
  7. > Named after [Dexter](http://en.wikipedia.org/wiki/Dexter_Morgan), a show you should not watch until completion.
  8. ## API
  9. <!-- eslint-disable no-unused-vars -->
  10. ```js
  11. var morgan = require('morgan')
  12. ```
  13. ### morgan(format, options)
  14. Create a new morgan logger middleware function using the given `format` and `options`.
  15. The `format` argument may be a string of a predefined name (see below for the names),
  16. a string of a format string, or a function that will produce a log entry.
  17. The `format` function will be called with three arguments `tokens`, `req`, and `res`,
  18. where `tokens` is an object with all defined tokens, `req` is the HTTP request and `res`
  19. is the HTTP response. The function is expected to return a string that will be the log
  20. line, or `undefined` / `null` to skip logging.
  21. #### Using a predefined format string
  22. <!-- eslint-disable no-undef -->
  23. ```js
  24. morgan('tiny')
  25. ```
  26. #### Using format string of predefined tokens
  27. <!-- eslint-disable no-undef -->
  28. ```js
  29. morgan(':method :url :status :res[content-length] - :response-time ms')
  30. ```
  31. #### Using a custom format function
  32. <!-- eslint-disable no-undef -->
  33. ``` js
  34. morgan(function (tokens, req, res) {
  35. return [
  36. tokens.method(req, res),
  37. tokens.url(req, res),
  38. tokens.status(req, res),
  39. tokens.res(req, res, 'content-length'), '-',
  40. tokens['response-time'](req, res), 'ms'
  41. ].join(' ')
  42. })
  43. ```
  44. #### Options
  45. Morgan accepts these properties in the options object.
  46. ##### immediate
  47. Write log line on request instead of response. This means that a requests will
  48. be logged even if the server crashes, _but data from the response (like the
  49. response code, content length, etc.) cannot be logged_.
  50. ##### skip
  51. Function to determine if logging is skipped, defaults to `false`. This function
  52. will be called as `skip(req, res)`.
  53. <!-- eslint-disable no-undef -->
  54. ```js
  55. // EXAMPLE: only log error responses
  56. morgan('combined', {
  57. skip: function (req, res) { return res.statusCode < 400 }
  58. })
  59. ```
  60. ##### stream
  61. Output stream for writing log lines, defaults to `process.stdout`.
  62. #### Predefined Formats
  63. There are various pre-defined formats provided:
  64. ##### combined
  65. Standard Apache combined log output.
  66. ```
  67. :remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
  68. ```
  69. ##### common
  70. Standard Apache common log output.
  71. ```
  72. :remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]
  73. ```
  74. ##### dev
  75. Concise output colored by response status for development use. The `:status`
  76. token will be colored green for success codes, red for server error codes,
  77. yellow for client error codes, cyan for redirection codes, and uncolored
  78. for information codes.
  79. ```
  80. :method :url :status :response-time ms - :res[content-length]
  81. ```
  82. ##### short
  83. Shorter than default, also including response time.
  84. ```
  85. :remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms
  86. ```
  87. ##### tiny
  88. The minimal output.
  89. ```
  90. :method :url :status :res[content-length] - :response-time ms
  91. ```
  92. #### Tokens
  93. ##### Creating new tokens
  94. To define a token, simply invoke `morgan.token()` with the name and a callback function.
  95. This callback function is expected to return a string value. The value returned is then
  96. available as ":type" in this case:
  97. <!-- eslint-disable no-undef -->
  98. ```js
  99. morgan.token('type', function (req, res) { return req.headers['content-type'] })
  100. ```
  101. Calling `morgan.token()` using the same name as an existing token will overwrite that
  102. token definition.
  103. The token function is expected to be called with the arguments `req` and `res`, representing
  104. the HTTP request and HTTP response. Additionally, the token can accept further arguments of
  105. it's choosing to customize behavior.
  106. ##### :date[format]
  107. The current date and time in UTC. The available formats are:
  108. - `clf` for the common log format (`"10/Oct/2000:13:55:36 +0000"`)
  109. - `iso` for the common ISO 8601 date time format (`2000-10-10T13:55:36.000Z`)
  110. - `web` for the common RFC 1123 date time format (`Tue, 10 Oct 2000 13:55:36 GMT`)
  111. If no format is given, then the default is `web`.
  112. ##### :http-version
  113. The HTTP version of the request.
  114. ##### :method
  115. The HTTP method of the request.
  116. ##### :referrer
  117. The Referrer header of the request. This will use the standard mis-spelled Referer header if exists, otherwise Referrer.
  118. ##### :remote-addr
  119. The remote address of the request. This will use `req.ip`, otherwise the standard `req.connection.remoteAddress` value (socket address).
  120. ##### :remote-user
  121. The user authenticated as part of Basic auth for the request.
  122. ##### :req[header]
  123. The given `header` of the request. If the header is not present, the
  124. value will be displayed as `"-"` in the log.
  125. ##### :res[header]
  126. The given `header` of the response. If the header is not present, the
  127. value will be displayed as `"-"` in the log.
  128. ##### :response-time[digits]
  129. The time between the request coming into `morgan` and when the response
  130. headers are written, in milliseconds.
  131. The `digits` argument is a number that specifies the number of digits to
  132. include on the number, defaulting to `3`, which provides microsecond precision.
  133. ##### :status
  134. The status code of the response.
  135. If the request/response cycle completes before a response was sent to the
  136. client (for example, the TCP socket closed prematurely by a client aborting
  137. the request), then the status will be empty (displayed as `"-"` in the log).
  138. ##### :total-time[digits]
  139. The time between the request coming into `morgan` and when the response
  140. has finished being written out to the connection, in milliseconds.
  141. The `digits` argument is a number that specifies the number of digits to
  142. include on the number, defaulting to `3`, which provides microsecond precision.
  143. ##### :url
  144. The URL of the request. This will use `req.originalUrl` if exists, otherwise `req.url`.
  145. ##### :user-agent
  146. The contents of the User-Agent header of the request.
  147. ### morgan.compile(format)
  148. Compile a format string into a `format` function for use by `morgan`. A format string
  149. is a string that represents a single log line and can utilize token syntax.
  150. Tokens are references by `:token-name`. If tokens accept arguments, they can
  151. be passed using `[]`, for example: `:token-name[pretty]` would pass the string
  152. `'pretty'` as an argument to the token `token-name`.
  153. The function returned from `morgan.compile` takes three arguments `tokens`, `req`, and
  154. `res`, where `tokens` is object with all defined tokens, `req` is the HTTP request and
  155. `res` is the HTTP response. The function will return a string that will be the log line,
  156. or `undefined` / `null` to skip logging.
  157. Normally formats are defined using `morgan.format(name, format)`, but for certain
  158. advanced uses, this compile function is directly available.
  159. ## Examples
  160. ### express/connect
  161. Simple app that will log all request in the Apache combined format to STDOUT
  162. ```js
  163. var express = require('express')
  164. var morgan = require('morgan')
  165. var app = express()
  166. app.use(morgan('combined'))
  167. app.get('/', function (req, res) {
  168. res.send('hello, world!')
  169. })
  170. ```
  171. ### vanilla http server
  172. Simple app that will log all request in the Apache combined format to STDOUT
  173. ```js
  174. var finalhandler = require('finalhandler')
  175. var http = require('http')
  176. var morgan = require('morgan')
  177. // create "middleware"
  178. var logger = morgan('combined')
  179. http.createServer(function (req, res) {
  180. var done = finalhandler(req, res)
  181. logger(req, res, function (err) {
  182. if (err) return done(err)
  183. // respond to request
  184. res.setHeader('content-type', 'text/plain')
  185. res.end('hello, world!')
  186. })
  187. })
  188. ```
  189. ### write logs to a file
  190. #### single file
  191. Simple app that will log all requests in the Apache combined format to the file
  192. `access.log`.
  193. ```js
  194. var express = require('express')
  195. var fs = require('fs')
  196. var morgan = require('morgan')
  197. var path = require('path')
  198. var app = express()
  199. // create a write stream (in append mode)
  200. var accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' })
  201. // setup the logger
  202. app.use(morgan('combined', { stream: accessLogStream }))
  203. app.get('/', function (req, res) {
  204. res.send('hello, world!')
  205. })
  206. ```
  207. #### log file rotation
  208. Simple app that will log all requests in the Apache combined format to one log
  209. file per day in the `log/` directory using the
  210. [rotating-file-stream module](https://www.npmjs.com/package/rotating-file-stream).
  211. ```js
  212. var express = require('express')
  213. var morgan = require('morgan')
  214. var path = require('path')
  215. var rfs = require('rotating-file-stream') // version 2.x
  216. var app = express()
  217. // create a rotating write stream
  218. var accessLogStream = rfs.createStream('access.log', {
  219. interval: '1d', // rotate daily
  220. path: path.join(__dirname, 'log')
  221. })
  222. // setup the logger
  223. app.use(morgan('combined', { stream: accessLogStream }))
  224. app.get('/', function (req, res) {
  225. res.send('hello, world!')
  226. })
  227. ```
  228. ### split / dual logging
  229. The `morgan` middleware can be used as many times as needed, enabling
  230. combinations like:
  231. * Log entry on request and one on response
  232. * Log all requests to file, but errors to console
  233. * ... and more!
  234. Sample app that will log all requests to a file using Apache format, but
  235. error responses are logged to the console:
  236. ```js
  237. var express = require('express')
  238. var fs = require('fs')
  239. var morgan = require('morgan')
  240. var path = require('path')
  241. var app = express()
  242. // log only 4xx and 5xx responses to console
  243. app.use(morgan('dev', {
  244. skip: function (req, res) { return res.statusCode < 400 }
  245. }))
  246. // log all requests to access.log
  247. app.use(morgan('common', {
  248. stream: fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' })
  249. }))
  250. app.get('/', function (req, res) {
  251. res.send('hello, world!')
  252. })
  253. ```
  254. ### use custom token formats
  255. Sample app that will use custom token formats. This adds an ID to all requests and displays it using the `:id` token.
  256. ```js
  257. var express = require('express')
  258. var morgan = require('morgan')
  259. var uuid = require('node-uuid')
  260. morgan.token('id', function getId (req) {
  261. return req.id
  262. })
  263. var app = express()
  264. app.use(assignId)
  265. app.use(morgan(':id :method :url :response-time'))
  266. app.get('/', function (req, res) {
  267. res.send('hello, world!')
  268. })
  269. function assignId (req, res, next) {
  270. req.id = uuid.v4()
  271. next()
  272. }
  273. ```
  274. ## License
  275. [MIT](LICENSE)
  276. [coveralls-image]: https://badgen.net/coveralls/c/github/expressjs/morgan/master
  277. [coveralls-url]: https://coveralls.io/r/expressjs/morgan?branch=master
  278. [npm-downloads-image]: https://badgen.net/npm/dm/morgan
  279. [npm-url]: https://npmjs.org/package/morgan
  280. [npm-version-image]: https://badgen.net/npm/v/morgan
  281. [travis-image]: https://badgen.net/travis/expressjs/morgan/master
  282. [travis-url]: https://travis-ci.org/expressjs/morgan