UNPKG

2.78 kBMarkdownView Raw
1# serve-directory
2
3 Serves pages that contain directory listings for a given path. forked from expressjs/serve-directory
4
5
6## Install
7
8```sh
9$ npm install serve-directory
10```
11
12## ustage
13
14```js
15const serveDirectory = require('serve-directory')
16
17const directory = serveDirectory('wwwroot', options)
18```
19
20### serveDirectory(path, options)
21
22Returns middlware that serves an index of the directory in the given `path`.
23
24The `path` is based off the `req.url` value, so a `req.url` of `'/some/dir`
25with a `path` of `'public'` will look at `'public/some/dir'`. If you are using
26something like `express`, you can change the URL "base" with `app.use` (see
27the express example).
28
29### Options
30
31serveDirectory accepts these properties in the options object.
32
33### example (default)
34```js
35{
36 imports: {},
37 hidden: true,
38 relative: true,
39 process: [
40 {
41 accept: 'text/html',
42 render: _.path.join(__dirname, 'directory.html')
43 },
44 {
45 accept: 'text/plain',
46 render(data) {
47 return data.files.map(file => file.name).join('\n') + '\n'
48 }
49 },
50 {
51 accept: 'application/json',
52 render(data) {
53 return JSON.stringify(data.files.map(file => file.name))
54 }
55 }
56 ]
57}
58```
59### imports
60functions will pass to render function, see [lodash.template](https://lodash.com/docs/4.17.4#template)
61
62by default some usful functions will import automatically
63
64see [utils.js](https://github.com/fisker/serve-directory/tree/master/src/utils.js)
65
66### hidden
67hide hidden files(file/folder start with ".") , default `true`.
68
69### relative
70use relative url , default `true`.
71
72### process
73array list how data should be handled
74
75#### accept
76mime split with `,`, space will be trimed
77
78### render
79by default we use a compiled lodash.template function to render data
80
81see [lodash.template](https://lodash.com/docs/4.17.4#template)
82
83#### string
84
85 a path to a template file
86 a template string
87
88
89#### function
90 a custom render function
91
92#### falsy value
93 remove default render function
94
95### data
96data pass to the render function
97
98path(String):
99 physical path
100
101pathname(String):
102 decoded request pathname
103
104url(URL):
105 request URL object
106
107method(String):
108 request method
109
110responseType(String):
111 response mine-type / content-type
112
113directory(Array<fs.Stats>):
114 directory stats with additional info `path` `pathname` `url`
115
116files(Array<fs.Stats>):
117 directory files stats with additional info `name` `ext` `type` `url`
118
119
120### Serve directory indexes with vanilla node.js http server
121 see [example.js](https://github.com/fisker/serve-directory/tree/master/example.js)
122
123## License
124
125MIT © [fisker Cheung](https://github.com/fisker)
\No newline at end of file