Skip to content

Commit abe9d69

Browse files
committedApr 23, 2019
feat: pass req, res to opts.onNoMatch handler
1 parent 6120ecf commit abe9d69

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed
 

‎packages/sirv-cli/boot.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = function (dir, opts) {
3636
}
3737

3838
if (opts.single) {
39-
opts.onNoMatch = res => fn({ path:'/', headers:{} }, res, r => (r.statusCode=404,r.end()));
39+
opts.onNoMatch = (req, res) => (req.path='/',fn(req, res, r => (r.statusCode=404,r.end())));
4040
}
4141

4242
fn = sirv(dir, opts);

‎packages/sirv/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function find(uri, extns) {
3131
return data;
3232
}
3333

34-
function is404(res) {
34+
function is404(req, res) {
3535
return (res.statusCode=404,res.end());
3636
}
3737

@@ -83,7 +83,7 @@ module.exports = function (dir, opts={}) {
8383
let uri = decodeURIComponent(req.path || req.pathname || parser(req).pathname);
8484
let arr = uri.includes('.') ? [uri] : toAssume(uri, extensions);
8585
let file = arr.map(x => join(dir, x)).find(fs.existsSync);
86-
if (!file) return next ? next() : isNotFound(res);
86+
if (!file) return next ? next() : isNotFound(req, res);
8787

8888
let stats = fs.statSync(file);
8989
setHeaders(res, uri, stats);
@@ -118,7 +118,7 @@ module.exports = function (dir, opts={}) {
118118
return function (req, res, next) {
119119
let pathname = decodeURIComponent(req.path || req.pathname || parser(req).pathname);
120120
let data = find(pathname, extensions);
121-
if (!data) return next ? next() : isNotFound(res);
121+
if (!data) return next ? next() : isNotFound(req, res);
122122

123123
setHeaders(res, pathname, data.stats);
124124
send(req, res, data.abs, data.stats, data.headers);

‎packages/sirv/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Type: `Function`
105105

106106
A custom function to run if a file cannot be found for a given request. <br>By default, `sirv` will send a basic `(404) Not found` response.
107107

108-
The function receives the current `res <ServerResponse>` as its only argument.
108+
The function receives the current `req <IncomingMessage>, res <ServerResponse>` pair for as its two arguments.
109109

110110
> **Note:** This won't run if a `next` callback has been provided to the middleware; see [`sirv`](#sirvdir-opts) description.
111111

0 commit comments

Comments
 (0)
Please sign in to comment.