From 39f97c5de83927f61b4650396edfeffaf0abd03b Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sun, 12 Aug 2018 04:01:01 -0600 Subject: [PATCH] v1.0.2: created working example, updated docs --- README.md | 25 ++++++++++++++----------- example.js | 22 ++++++++++++++++++++++ package.json | 6 +++++- 3 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 example.js diff --git a/README.md b/README.md index 59ca1a1..ee640fe 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/example.js b/example.js new file mode 100644 index 0000000..ff238c6 --- /dev/null +++ b/example.js @@ -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()); }); +} diff --git a/package.json b/package.json index ac687aa..cc2ae2e 100644 --- a/package.json +++ b/package.json @@ -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" } }