Skip to content

Commit

Permalink
fix(server-middleware): allow res.end -> next() in middeware flow whe…
Browse files Browse the repository at this point in the history
…n no serveStatic exists - fixes #1481
  • Loading branch information
shakyShane committed Jan 5, 2018
1 parent 148ac02 commit 620e9ae
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/server/utils.js
Expand Up @@ -241,20 +241,23 @@ var serverUtils = {
return !x.override;
})
.concat(
bs.options.get("mode") !== "proxy" && {
id: "Browsersync 404/index support",
route: "",
handle: serveIndex(bs.options.get("cwd"), {
icons: true,
view: "details"
})
}
bs.options.get("mode") !== "proxy" &&
userMiddlewares.length === 0 && {
id: "Browsersync 404/index support",
route: "",
handle: serveIndex(bs.options.get("cwd"), {
icons: true,
view: "details"
})
}
);

return []
const mwStack = []
.concat(beforeMiddlewares, defaultMiddlewares, afterMiddlewares)
.filter(Boolean);

return mwStack;

function normaliseMiddleware(item) {
/**
* Object given in options, which
Expand Down
37 changes: 37 additions & 0 deletions test/specs/e2e/middleware/middleware.server.option.js
Expand Up @@ -189,3 +189,40 @@ describe("Accepting multiple server middlewares as top-level option", function()
});
});
});

describe("Allow middlewares to call next() after res.end if no server provided", function() {
var bs;

before(function(done) {
browserSync.reset();

var fn = function(req, res, next) {
res.write('bs');
res.end();
next();
};

var config = {
logLevel: "silent",
open: false,
middleware: fn
};

bs = browserSync.init(config, done).instance;
});

after(function() {
bs.cleanup();
});

it("should call the middlewares", function(done) {
request(bs.server)
.get("/")
.set("accept", "text/html")
.expect(200)
.end(function(err, res) {
assert.include(res.text, 'bs');
done();
});
});
});

0 comments on commit 620e9ae

Please sign in to comment.