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
|
15 | const serveDirectory = require('serve-directory')
|
16 |
|
17 | const directory = serveDirectory('wwwroot', options)
|
18 | ```
|
19 |
|
20 | ### serveDirectory(path, options)
|
21 |
|
22 | Returns middlware that serves an index of the directory in the given `path`.
|
23 |
|
24 | The `path` is based off the `req.url` value, so a `req.url` of `'/some/dir`
|
25 | with a `path` of `'public'` will look at `'public/some/dir'`. If you are using
|
26 | something like `express`, you can change the URL "base" with `app.use` (see
|
27 | the express example).
|
28 |
|
29 | ### Options
|
30 |
|
31 | serveDirectory 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
|
60 | functions will pass to render function, see [lodash.template](https://lodash.com/docs/4.17.4#template)
|
61 |
|
62 | by default some usful functions will import automatically
|
63 |
|
64 | see [utils.js](https://github.com/fisker/serve-directory/tree/master/src/utils.js)
|
65 |
|
66 | ### hidden
|
67 | hide hidden files(file/folder start with ".") , default `true`.
|
68 |
|
69 | ### relative
|
70 | use relative url , default `true`.
|
71 |
|
72 | ### process
|
73 | array list how data should be handled
|
74 |
|
75 | #### accept
|
76 | mime split with `,`, space will be trimed
|
77 |
|
78 | ### render
|
79 | by default we use a compiled lodash.template function to render data
|
80 |
|
81 | see [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
|
96 | data pass to the render function
|
97 |
|
98 | path(String):
|
99 | physical path
|
100 |
|
101 | pathname(String):
|
102 | decoded request pathname
|
103 |
|
104 | url(URL):
|
105 | request URL object
|
106 |
|
107 | method(String):
|
108 | request method
|
109 |
|
110 | responseType(String):
|
111 | response mine-type / content-type
|
112 |
|
113 | directory(Array<fs.Stats>):
|
114 | directory stats with additional info `path` `pathname` `url`
|
115 |
|
116 | files(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 |
|
125 | MIT © [fisker Cheung](https://github.com/fisker) |
\ | No newline at end of file |