Skip to content

Commit

Permalink
feat: add option "serveIndex" to enable/disable serveIndex middleware (
Browse files Browse the repository at this point in the history
  • Loading branch information
EslamHiko authored and evilebottnawi committed Apr 5, 2019
1 parent abf8691 commit d5d60cb
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
5 changes: 5 additions & 0 deletions bin/options.js
Expand Up @@ -21,6 +21,11 @@ const options = {
type: 'boolean',
describe: 'Lazy',
},
serveIndex: {
type: 'boolean',
describe: 'Enables/Disables serveIndex middleware',
default: true,
},
inline: {
type: 'boolean',
default: true,
Expand Down
10 changes: 9 additions & 1 deletion lib/Server.js
Expand Up @@ -106,6 +106,8 @@ class Server {
this.headers = options.headers;
this.progress = options.progress;

this.serveIndex = options.serveIndex;

this.clientOverlay = options.overlay;
this.clientLogLevel = options.clientLogLevel;

Expand Down Expand Up @@ -557,9 +559,15 @@ class Server {

defaultFeatures.push('magicHtml');

if (contentBase !== false) {
// checking if it's set to true or not set (Default : undefined => true)
this.serveIndex = this.serveIndex || this.serveIndex === undefined;

const shouldHandleServeIndex = contentBase && this.serveIndex;

if (shouldHandleServeIndex) {
defaultFeatures.push('contentBaseIndex');
}

// compress is placed last and uses unshift so that it will be the first middleware used
if (options.compress) {
defaultFeatures.unshift('compress');
Expand Down
3 changes: 3 additions & 0 deletions lib/options.json
@@ -1,6 +1,9 @@
{
"type": "object",
"properties": {
"serveIndex": {
"type": "boolean"
},
"hot": {
"type": "boolean"
},
Expand Down
82 changes: 82 additions & 0 deletions test/ContentBase.test.js
Expand Up @@ -64,7 +64,89 @@ describe('ContentBase', () => {
}, 1000);
});
});
describe('test listing files in folders without index.html using the option serveIndex:false', () => {
beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
watchContentBase: true,
serveIndex: false,
},
done
);
req = request(server.app);
});

afterAll((done) => {
helper.close(() => {
done();
});
});

it("shouldn't list the files inside the assets folder (404)", (done) => {
req.get('/assets/').expect(404, done);
});

it('should show Heyo. because bar has index.html inside it (200)', (done) => {
req.get('/bar/').expect(200, /Heyo/, done);
});
});
describe('test listing files in folders without index.html using the option serveIndex:true', () => {
beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
watchContentBase: true,
serveIndex: true,
},
done
);
req = request(server.app);
});

afterAll((done) => {
helper.close(() => {
done();
});
});

it('should list the files inside the assets folder (200)', (done) => {
req.get('/assets/').expect(200, done);
});

it('should show Heyo. because bar has index.html inside it (200)', (done) => {
req.get('/bar/').expect(200, /Heyo/, done);
});
});
describe('test listing files in folders without index.html using the option serveIndex default (true)', () => {
beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
watchContentBase: true,
},
done
);
req = request(server.app);
});

afterAll((done) => {
helper.close(() => {
done();
});
});

it('should list the files inside the assets folder (200)', (done) => {
req.get('/assets/').expect(200, done);
});

it('should show Heyo. because bar has index.html inside it (200)', (done) => {
req.get('/bar/').expect(200, /Heyo/, done);
});
});
describe('to directories', () => {
beforeAll((done) => {
server = helper.start(
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/contentbase-config/public/bar/index.html
@@ -0,0 +1 @@
Heyo

0 comments on commit d5d60cb

Please sign in to comment.