@ceicc/range
Advanced tools
Comparing version 2.0.1 to 2.1.0
23
index.js
@@ -25,2 +25,10 @@ const | ||
* @property {boolean} [lastModified] add last-modified header - default true | ||
* @property {string|boolean} [notFound] a handler for non existing files | ||
* | ||
* `notFound: false` a rejection will be thrown (default). | ||
* | ||
* `notFound: true` empty body with response code '404' will be sent. | ||
* | ||
* `notFound: <string>` send a file with response code '404', the given string is the path to file. | ||
* if the path doesn't led to a file, a rejection will be thrown | ||
*/ | ||
@@ -46,2 +54,17 @@ | ||
if (options?.notFound === true) { | ||
res.statusCode = 404; | ||
res.end(); | ||
resolve(404); | ||
return false; | ||
} | ||
if (typeof options?.notFound === "string") { | ||
range(options.notFound, res, { | ||
...options, | ||
notFound: false | ||
}).then(resolve).catch(rejects); | ||
return false; | ||
} | ||
const e = new Error("File Not Found"); | ||
@@ -48,0 +71,0 @@ e.code = 404; |
{ | ||
"name": "@ceicc/range", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "http range request handler", | ||
@@ -8,2 +8,3 @@ "main": "index.js", | ||
"dev": "nodemon test/server.js", | ||
"app-dev": "nodemon test/app.js", | ||
"start": "node index.js" | ||
@@ -10,0 +11,0 @@ }, |
@@ -38,12 +38,18 @@ # range | ||
4. `conditional` whether to respect conditional requests or not - default false | ||
* if true, the headers object is required | ||
4. `conditional` whether to respect conditional requests or not - default false | ||
if true, the headers object is required | ||
5. `range` accept range request - default false | ||
* if true, the headers object is required | ||
5. `range` accept range request - default false | ||
if true, the headers object is required | ||
6. `headers` the request headers object `req.headers` | ||
* if `range` and/or `conditionalRequest` are true, then the headers object is required. | ||
* you can pass the whole headers object, or only the conditional and range headers | ||
6. `headers` the request headers object `req.headers` | ||
if `range` and/or `conditionalRequest` are true, then the headers object is required. | ||
you can pass the whole headers object, or only the conditional and range headers. | ||
7. `notFound` handler for non existing files | ||
`notFound: false` - a rejection will be thrown (default). | ||
`notFound: true` - empty body with response code '404' will be sent. | ||
`notFound: <string>` - send a file with response code '404', the given string is the path to file. | ||
if the path doesn't led to a file, a rejection will be thrown | ||
# Resolves | ||
@@ -61,11 +67,9 @@ the response status code | ||
express = require("express"), | ||
app = express(), | ||
range = require("@ceicc/range"); | ||
range = require("@ceicc/range"), | ||
app = express(); | ||
app.get('/', (req, res, next) => { | ||
range('./public/index.html', res).catch(next); | ||
}); | ||
app.get('/', (req, res, next) => range('./public/index.html', res).catch(next)); | ||
app.get('/public/*', (req, res, next) => { | ||
range(__dirname + req.path, res, { | ||
range('.' + req.path, res, { | ||
headers: req.headers, | ||
@@ -75,2 +79,3 @@ range: true, | ||
maxAge: 2592000, // 30 Days | ||
notFound: './test/public/404.html', | ||
}).catch(next); | ||
@@ -80,9 +85,5 @@ }); | ||
app.use((err, req, res, next) => { | ||
if (err.code === 404) | ||
return res.status(404).send(); | ||
console.error(err); | ||
console.dir(err); | ||
if (!res.headersSent) | ||
res.status(500).send(); | ||
res.sendStatus(500); | ||
}); | ||
@@ -89,0 +90,0 @@ |
10281
151
89