v1.0.2: created working example, updated docs

This commit is contained in:
AJ ONeal 2018-08-12 04:01:01 -06:00
parent 33c89f136e
commit 39f97c5de8
3 changed files with 41 additions and 12 deletions

View File

@ -1,28 +1,31 @@
serve-tpl-attachment
==================
```js
var serveIndex = require('serve-index')({
template: require('serve-tpl-attachment')
});
```
A fork of the original `serve-index` template that, in combination with `serve-static`,
provides support for direct file downloads (using the Content-Disposition attachment header).
```js
var express = require('express');
var app = express();
### Example Usage
var serveIndex = require('serve-index');
```js
var serveTpl = require('serve-tpl-attachment');
var serveDirs = serveIndex({ template: serveTpl() });
var serveIndex = require('serve-index')('./public', { template: serveTpl() });
app.use('/', function (req, res, next) {
// enable direct downloads for express.static()
if (req.query.download) {
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-Disposition', 'attachment; filename="'+path.basename(req.url)+'"');
res.setHeader('Content-Disposition', 'attachment; filename="'+
path.basename(req.url.replace(/\?.*/, ''))
+'"');
}
express.static('./public')(req, res, function () {
serveDirs(req, res, next);
});
});
next();
}, express.static('./public'), serveIndex);
```
Additional Options

22
example.js Normal file
View File

@ -0,0 +1,22 @@
'use strict';
var path = require('path');
var express = require('express');
var app = express();
//var serveTpl = require('serve-tpl-attachment');
var serveTpl = require('./');
var serveIndex = require('serve-index')(__dirname, { template: serveTpl() });
app.use('/', function (req, res, next) {
// enable direct downloads for express.static()
if (req.query.download) {
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-Disposition', 'attachment; filename="'+path.basename(req.url.replace(/\?.*/, ''))+'"');
}
next();
}, express.static(__dirname), serveIndex);
module.exports = app;
if (module === require.main) {
require('http').createServer(app).listen(4080, function () { console.log(this.address()); });
}

View File

@ -1,6 +1,6 @@
{
"name": "serve-tpl-attachment",
"version": "1.0.1",
"version": "1.0.2",
"description": "A template for serve-static with a direct download option (requires serve-index)",
"homepage": "https://git.coolaj86.com/coolaj86/serve-tpl-download.js",
"main": "index.js",
@ -21,5 +21,9 @@
"accepts": "^1.3.5",
"batch": "^0.6.1",
"mime-types": "^2.1.19"
},
"devDependencies": {
"express": "^4.16.3",
"serve-index": "^1.9.1"
}
}