v1.0.2: created working example, updated docs
This commit is contained in:
parent
33c89f136e
commit
39f97c5de8
25
README.md
25
README.md
|
@ -1,28 +1,31 @@
|
||||||
serve-tpl-attachment
|
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`,
|
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).
|
provides support for direct file downloads (using the Content-Disposition attachment header).
|
||||||
|
|
||||||
```js
|
### Example Usage
|
||||||
var express = require('express');
|
|
||||||
var app = express();
|
|
||||||
|
|
||||||
var serveIndex = require('serve-index');
|
```js
|
||||||
var serveTpl = require('serve-tpl-attachment');
|
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) {
|
app.use('/', function (req, res, next) {
|
||||||
|
|
||||||
// enable direct downloads for express.static()
|
// enable direct downloads for express.static()
|
||||||
if (req.query.download) {
|
if (req.query.download) {
|
||||||
res.setHeader('Content-Type', 'application/octet-stream');
|
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 () {
|
next();
|
||||||
serveDirs(req, res, next);
|
}, express.static('./public'), serveIndex);
|
||||||
});
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Additional Options
|
Additional Options
|
||||||
|
|
|
@ -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()); });
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "serve-tpl-attachment",
|
"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)",
|
"description": "A template for serve-static with a direct download option (requires serve-index)",
|
||||||
"homepage": "https://git.coolaj86.com/coolaj86/serve-tpl-download.js",
|
"homepage": "https://git.coolaj86.com/coolaj86/serve-tpl-download.js",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
@ -21,5 +21,9 @@
|
||||||
"accepts": "^1.3.5",
|
"accepts": "^1.3.5",
|
||||||
"batch": "^0.6.1",
|
"batch": "^0.6.1",
|
||||||
"mime-types": "^2.1.19"
|
"mime-types": "^2.1.19"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"express": "^4.16.3",
|
||||||
|
"serve-index": "^1.9.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue