Skip to content

Commit c5a1049

Browse files
committed
feat: add directoryCallback option
1 parent b28067a commit c5a1049

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- Fix: ensure `--default-extension` and `--server-info` are settable by CLI
3535
- Fix: change `fs.createReadStream()` mode to integer (@pixcai)
3636
- Enhancement: TypeScript support
37+
- Enhancement: Add `directoryCallback` option
3738
- Enhancement: Allow access with local ip (@flyingsky)
3839
- Enhancement: Allow `serverInfo` to be `null` (@martindale)
3940
- Enhancement: Time display logging with leading 0 (@mauris)

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@ const server = http.createServer((req, res) => {
152152
}).listen(9010);
153153
```
154154

155+
### Serving custom directory
156+
157+
```js
158+
fileServer = new statik.Server(__dirname + '/../fixtures', {
159+
directoryCallback (pathname, req, res) {
160+
res.writeHead(200, {
161+
'Content-Type': 'text/html'
162+
});
163+
res.end(`Hi <b>${basename(pathname)}</b>!`);
164+
165+
// You could add a listing of files by `readdir` here
166+
}
167+
});
168+
```
169+
155170
### Intercepting errors & Listening
156171

157172
An optional callback can be passed as last argument, it will be called every

lib/node-static.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ function tryStat(p, callback) {
5959
/**
6060
* @typedef {{
6161
* indexFile?: string,
62+
* directoryCallback?: (
63+
* pathname: string,
64+
* req: http.IncomingMessage,
65+
* res: http.ServerResponse<http.IncomingMessage> & {
66+
* req: http.IncomingMessage;
67+
* }
68+
* ) => void,
6269
* gzip?: boolean|RegExp,
6370
* headers?: http.OutgoingHttpHeaders,
6471
* serverInfo?: string|null,
@@ -138,6 +145,11 @@ class Server extends events.EventEmitter {
138145
* @param {(status: number, headers: http.OutgoingHttpHeaders) => void} finish
139146
*/
140147
serveDir (pathname, req, res, finish) {
148+
if (this.options.directoryCallback) {
149+
this.options.directoryCallback(pathname, req, res);
150+
return;
151+
}
152+
141153
const htmlIndex = path.join(pathname, this.options.indexFile);
142154

143155
tryStat(htmlIndex, (e, stat) => {

test/integration/node-static-test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import http from 'http';
2+
import {basename} from 'path';
23

34
import {assert} from 'chai';
45
import fetch from 'node-fetch';
@@ -725,13 +726,38 @@ describe('node-static', function () {
725726
this.server.close();
726727
});
727728

728-
it('returns 200 with missing JSON file', async function () {
729+
it('returns 200 with multiple JSON files config', async function () {
729730
const response = await fetch(this.getTestServer() + '/');
730731
assert.equal(response.status, 200, 'should respond with 200');
731732
assert.equal(await response.text(), 'Hi\nhello world', 'should respond with Hi\nhello world');
732733
});
733734
});
734735

736+
describe('once an http server is listening with directoryCallback', function () {
737+
before(function () {
738+
fileServer = new statik.Server(__dirname + '/../fixtures', {
739+
directoryCallback (pathname, req, res) {
740+
res.writeHead(200, {
741+
'Content-Type': 'text/html'
742+
});
743+
res.end(`Hi ${basename(pathname)}!`);
744+
}
745+
});
746+
});
747+
beforeEach(async function () {
748+
await setupStaticServer(this);
749+
});
750+
afterEach(async function () {
751+
this.server.close();
752+
});
753+
754+
it('returns 200 with directoryCallback', async function () {
755+
const response = await fetch(this.getTestServer() + '/there');
756+
assert.equal(response.status, 200, 'should respond with 200');
757+
assert.equal(await response.text(), 'Hi there!', 'should respond with Hi there!');
758+
});
759+
});
760+
735761
describe('once an http server is listening with bad JSON files configuration', function () {
736762
before(function () {
737763
fileServer = new statik.Server(__dirname + '/../fixtures/index-with-bad-json');

0 commit comments

Comments
 (0)