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.

220 lines
7.6 KiB

8 years ago
  1. # Ecstatic [![build status](https://secure.travis-ci.org/jfhbrook/node-ecstatic.png)](http://travis-ci.org/jfhbrook/node-ecstatic)
  2. ![](http://imgur.com/vhub5.png)
  3. A simple static file server middleware. Use it with a raw http server or
  4. express/connect!
  5. # Examples:
  6. ## express 3.0.x
  7. ``` js
  8. var http = require('http');
  9. var express = require('express');
  10. var ecstatic = require('ecstatic');
  11. var app = express();
  12. app.use(ecstatic({ root: __dirname + '/public' }));
  13. http.createServer(app).listen(8080);
  14. console.log('Listening on :8080');
  15. ```
  16. ## stock http server
  17. ``` js
  18. var http = require('http');
  19. var ecstatic = require('ecstatic');
  20. http.createServer(
  21. ecstatic({ root: __dirname + '/public' })
  22. ).listen(8080);
  23. console.log('Listening on :8080');
  24. ```
  25. ### fall through
  26. To allow fall through to your custom routes:
  27. ```js
  28. ecstatic({ root: __dirname + '/public', handleError: false })
  29. ```
  30. # API:
  31. ## ecstatic(opts);
  32. Pass ecstatic an options hash, and it will return your middleware!
  33. ```js
  34. var opts = {
  35. root : __dirname + '/public',
  36. port : 8000,
  37. baseDir : '/',
  38. cache : 3600,
  39. showDir : true,
  40. showDotfiles : true,
  41. autoIndex : false,
  42. humanReadable : true,
  43. headers : {},
  44. si : false,
  45. defaultExt : 'html',
  46. gzip : false,
  47. serverHeader : true,
  48. contentType : 'application/octet-stream',
  49. mimeTypes : undefined,
  50. handleOptionsMethod: false
  51. }
  52. ```
  53. If `opts` is a string, the string is assigned to the root folder and all other
  54. options are set to their defaults.
  55. ### `opts.root`
  56. `opts.root` is the directory you want to serve up.
  57. ### `opts.port`
  58. `opts.port` is the port you want ecstatic to listen to. Defaults to 8000.
  59. ### `opts.baseDir`
  60. `opts.baseDir` is `/` by default, but can be changed to allow your static files
  61. to be served off a specific route. For example, if `opts.baseDir === "blog"`
  62. and `opts.root = "./public"`, requests for `localhost:8080/blog/index.html` will
  63. resolve to `./public/index.html`.
  64. ### `opts.cache`
  65. Customize cache control with `opts.cache` , if it is a number then it will set max-age in seconds.
  66. Other wise it will pass through directly to cache-control. Time defaults to 3600 s (ie, 1 hour).
  67. If it is a function, it will be executed on every request, and passed the pathname. Whatever it returns, string or number, will be used as the cache control header like above.
  68. ### `opts.showDir`
  69. Turn **off** directory listings with `opts.showDir === false`. Defaults to **true**.
  70. ### `opts.showDotfiles`
  71. Exclude dotfiles from directory listings with `opts.showDotfiles === false`. Defaults to **true**.
  72. ### `opts.humanReadable`
  73. If showDir is enabled, add human-readable file sizes. Defaults to **true**.
  74. Aliases are `humanreadable` and `human-readable`.
  75. ### `opts.headers`
  76. Set headers on every response. `opts.headers` can be an object mapping string
  77. header names to string header values, a colon (:) separated string, or an array
  78. of colon separated strings.
  79. `opts.H` and `opts.header` are aliased to `opts.headers` so that you can use
  80. `-H` and `--header` options to set headers on the command-line like curl:
  81. ``` sh
  82. $ ecstatic ./public -p 5000 -H 'Access-Control-Allow-Origin: *'
  83. ```
  84. ### `opts.si`
  85. If showDir and humanReadable are enabled, print file sizes with base 1000 instead
  86. of base 1024. Name is inferred from cli options for `ls`. Aliased to `index`, the
  87. equivalent option in Apache.
  88. ### `opts.autoIndex`
  89. Serve `/path/index.html` when `/path/` is requested.
  90. Turn **off** autoIndexing with `opts.autoIndex === false`. Defaults to **true**.
  91. ### `opts.defaultExt`
  92. Turn on default file extensions with `opts.defaultExt`. If `opts.defaultExt` is
  93. true, it will default to `html`. For example if you want a request to `/a-file`
  94. to resolve to `./public/a-file.html`, set this to `true`. If you want
  95. `/a-file` to resolve to `./public/a-file.json` instead, set `opts.defaultExt` to
  96. `json`.
  97. ### `opts.gzip`
  98. Set `opts.gzip === true` in order to turn on "gzip mode," wherein ecstatic will
  99. serve `./public/some-file.js.gz` in place of `./public/some-file.js` when the
  100. gzipped version exists and ecstatic determines that the behavior is appropriate.
  101. ### `opts.serverHeader`
  102. Set `opts.serverHeader` to false in order to turn off setting the `Server` header
  103. on all responses served by ecstatic.
  104. ### `opts.contentType`
  105. Set `opts.contentType` in order to change default Content-Type header value.
  106. Defaults to **application/octet-stream**.
  107. ### `opts.mimeTypes`
  108. Add new or override one or more mime-types. This affects the HTTP Content-Type header.
  109. Can either be a path to a [`.types`](http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types) file or an object hash of type(s).
  110. ecstatic({ mimeType: { 'mime-type': ['file_extension', 'file_extension'] } })
  111. ### `opts.handleError`
  112. Turn **off** handleErrors to allow fall-through with `opts.handleError === false`, Defaults to **true**.
  113. ### `opts.weakEtags`
  114. Set `opts.weakEtags` to true in order to generate weak etags instead of strong etags. Defaults to **false**. See `opts.weakCompare` as well.
  115. ### `opts.weakCompare`
  116. Turn **on** weakCompare to allow the weak comparison function for etag validation. Defaults to **false**.
  117. See https://www.ietf.org/rfc/rfc2616.txt Section 13.3.3 for more details.
  118. ### `opts.handleOptionsMethod`
  119. Set handleOptionsMethod to true in order to respond to 'OPTIONS' calls with any standard/set headers. Defaults to **false**. Useful for hacking up CORS support.
  120. ### `opts.cors`
  121. This is a **convenience** setting which turns on `handleOptionsMethod` and sets the headers **Access-Control-Allow-Origin: \*** and **Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since**. This *should* be enough to quickly make cross-origin resource sharing work between development APIs. More advanced usage can come either from overriding these headers with the headers argument, or by using the `handleOptionsMethod` flag and then setting headers "manually." Alternately, just do it in your app using separate middlewares/abstractions.
  122. Defaults to **false**.
  123. ## middleware(req, res, next);
  124. This works more or less as you'd expect.
  125. ### ecstatic.showDir(folder);
  126. This returns another middleware which will attempt to show a directory view. Turning on auto-indexing is roughly equivalent to adding this middleware after an ecstatic middleware with autoindexing disabled.
  127. ### `ecstatic` command
  128. to start a standalone static http server,
  129. run `npm install -g ecstatic` and then run `ecstatic [dir?] [options] --port PORT`
  130. all options work as above, passed in [optimist](https://github.com/substack/node-optimist) style.
  131. `port` defaults to `8000`. If a `dir` or `--root dir` argument is not passed, ecsatic will
  132. serve the current dir. Ecstatic also respects the PORT environment variable.
  133. # Tests:
  134. Ecstatic has a fairly extensive test suite. You can run it with:
  135. ```sh
  136. $ npm test
  137. ```
  138. # Contribute:
  139. Without outside contributions, ecstatic would wither and die! Before
  140. contributing, take a quick look at the contributing guidelines in
  141. [./CONTRIBUTING.md](./CONTRIBUTING.md) . They're relatively painless, I promise.
  142. For Windows users, it is especially important to read the [./CONTRIBUTING.md](./CONTRIBUTING.md)
  143. section as you can **not** clone ecstatic without changing some settings in git.
  144. # License:
  145. MIT. See LICENSE.txt. For contributors, see CONTRIBUTORS.md