Skip to content

Commit 712040d

Browse files
cecchishellscape
authored andcommitted
Stop serving index.html if options.index is falsy (#154)
* Stop serving index.html if options.index is falsy * Documentation improvements * Add tests
1 parent e33087c commit 712040d

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ app.use(webpackMiddleware(webpack({
6969
publicPath: "/assets/",
7070
// public path to bind the middleware to
7171
// use the same as in webpack
72-
72+
7373
index: "index.html",
74-
// the index path for web server
74+
// The index path for web server, defaults to "index.html".
75+
// If falsy (but not undefined), the server will not respond to requests to the root URL.
7576

7677
headers: { "X-Custom-Header": "yes" },
7778
// custom headers

middleware.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ module.exports = function(compiler, options) {
4747
var stat = context.fs.statSync(filename);
4848
if(!stat.isFile()) {
4949
if(stat.isDirectory()) {
50-
filename = pathJoin(filename, context.options.index || "index.html");
50+
var index = context.options.index;
51+
52+
if(index === undefined || index === true) {
53+
index = "index.html";
54+
} else if(!index) {
55+
throw "next";
56+
}
57+
58+
filename = pathJoin(filename, index);
5159
stat = context.fs.statSync(filename);
5260
if(!stat.isFile()) throw "next";
5361
} else {

test/Server.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,27 @@ describe("Server", function() {
101101
});
102102
});
103103

104+
describe("no index mode", function() {
105+
before(function(done) {
106+
app = express();
107+
var compiler = webpack(webpackConfig);
108+
app.use(middleware(compiler, {
109+
stats: "errors-only",
110+
quiet: true,
111+
index: false,
112+
publicPath: "/",
113+
}));
114+
listen = listenShorthand(done);
115+
});
116+
after(close);
117+
118+
it("request to directory", function(done) {
119+
request(app).get("/")
120+
.expect("Content-Type", "text/html; charset=utf-8")
121+
.expect(404, done);
122+
});
123+
});
124+
104125
describe("lazy mode", function() {
105126
before(function(done) {
106127
app = express();

0 commit comments

Comments
 (0)