Skip to content

Commit d5d60cb

Browse files
EslamHikoevilebottnawi
authored andcommittedApr 5, 2019
feat: add option "serveIndex" to enable/disable serveIndex middleware (#1752)
1 parent abf8691 commit d5d60cb

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed
 

Diff for: ‎bin/options.js

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ const options = {
2121
type: 'boolean',
2222
describe: 'Lazy',
2323
},
24+
serveIndex: {
25+
type: 'boolean',
26+
describe: 'Enables/Disables serveIndex middleware',
27+
default: true,
28+
},
2429
inline: {
2530
type: 'boolean',
2631
default: true,

Diff for: ‎lib/Server.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class Server {
106106
this.headers = options.headers;
107107
this.progress = options.progress;
108108

109+
this.serveIndex = options.serveIndex;
110+
109111
this.clientOverlay = options.overlay;
110112
this.clientLogLevel = options.clientLogLevel;
111113

@@ -557,9 +559,15 @@ class Server {
557559

558560
defaultFeatures.push('magicHtml');
559561

560-
if (contentBase !== false) {
562+
// checking if it's set to true or not set (Default : undefined => true)
563+
this.serveIndex = this.serveIndex || this.serveIndex === undefined;
564+
565+
const shouldHandleServeIndex = contentBase && this.serveIndex;
566+
567+
if (shouldHandleServeIndex) {
561568
defaultFeatures.push('contentBaseIndex');
562569
}
570+
563571
// compress is placed last and uses unshift so that it will be the first middleware used
564572
if (options.compress) {
565573
defaultFeatures.unshift('compress');

Diff for: ‎lib/options.json

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"type": "object",
33
"properties": {
4+
"serveIndex": {
5+
"type": "boolean"
6+
},
47
"hot": {
58
"type": "boolean"
69
},

Diff for: ‎test/ContentBase.test.js

+82
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,89 @@ describe('ContentBase', () => {
6464
}, 1000);
6565
});
6666
});
67+
describe('test listing files in folders without index.html using the option serveIndex:false', () => {
68+
beforeAll((done) => {
69+
server = helper.start(
70+
config,
71+
{
72+
contentBase: contentBasePublic,
73+
watchContentBase: true,
74+
serveIndex: false,
75+
},
76+
done
77+
);
78+
req = request(server.app);
79+
});
80+
81+
afterAll((done) => {
82+
helper.close(() => {
83+
done();
84+
});
85+
});
86+
87+
it("shouldn't list the files inside the assets folder (404)", (done) => {
88+
req.get('/assets/').expect(404, done);
89+
});
90+
91+
it('should show Heyo. because bar has index.html inside it (200)', (done) => {
92+
req.get('/bar/').expect(200, /Heyo/, done);
93+
});
94+
});
95+
describe('test listing files in folders without index.html using the option serveIndex:true', () => {
96+
beforeAll((done) => {
97+
server = helper.start(
98+
config,
99+
{
100+
contentBase: contentBasePublic,
101+
watchContentBase: true,
102+
serveIndex: true,
103+
},
104+
done
105+
);
106+
req = request(server.app);
107+
});
67108

109+
afterAll((done) => {
110+
helper.close(() => {
111+
done();
112+
});
113+
});
114+
115+
it('should list the files inside the assets folder (200)', (done) => {
116+
req.get('/assets/').expect(200, done);
117+
});
118+
119+
it('should show Heyo. because bar has index.html inside it (200)', (done) => {
120+
req.get('/bar/').expect(200, /Heyo/, done);
121+
});
122+
});
123+
describe('test listing files in folders without index.html using the option serveIndex default (true)', () => {
124+
beforeAll((done) => {
125+
server = helper.start(
126+
config,
127+
{
128+
contentBase: contentBasePublic,
129+
watchContentBase: true,
130+
},
131+
done
132+
);
133+
req = request(server.app);
134+
});
135+
136+
afterAll((done) => {
137+
helper.close(() => {
138+
done();
139+
});
140+
});
141+
142+
it('should list the files inside the assets folder (200)', (done) => {
143+
req.get('/assets/').expect(200, done);
144+
});
145+
146+
it('should show Heyo. because bar has index.html inside it (200)', (done) => {
147+
req.get('/bar/').expect(200, /Heyo/, done);
148+
});
149+
});
68150
describe('to directories', () => {
69151
beforeAll((done) => {
70152
server = helper.start(
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Heyo

0 commit comments

Comments
 (0)
Please sign in to comment.