Skip to content

Commit

Permalink
Receiving options and passing to send to koa-send
Browse files Browse the repository at this point in the history
Updating README

updating README

updating README

removing a "1"

Combined examples of koa-send options with default usage.
  • Loading branch information
zanaca authored and janpieterz committed Nov 29, 2015
1 parent 47ad3f8 commit e46ae6b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

Middleware for KoaJS to serve a folder as static files. Other implementations require every file to be appointed individually (as far as I know). The software initializes at the startup of your application, so changes made in the underlying folders require a restart of your app.


## Options

- `maxage` Browser cache max-age in milliseconds. defaults to 0
- `hidden` Allow transfer of hidden files. defaults to false
- `gzip` Try to serve the gzipped version of a file automatically when `gzip` is supported by a client and if the requested file with `.gz` extension exists. defaults to true.
- `format` If not `false` (defaults to `true`), format the path to serve static file servers and not require a trailing slash for directories, so that you can do both `/directory` and `/directory/`


## Example
```js
var serve = require('koa-static-folder'),
koa = require('koa'),
app = koa();

app.use(serve('./public'));
app.use(serve('./assets'));
app.use(serve('./assets', {maxage: 5 * 60 * 1000}));

app.use(function *(next){
if('/' == this.path){
Expand All @@ -25,7 +34,8 @@ console.log('Koa server listening at port 8000');
MIT

## Release log
0.1.6 - Added the possibility to inform max-age for caching.
0.1.5 - Updated documentation slightly.
0.1.4 - Updated documentation slightly.
0.1.3 - Using procedd.cwd() instead of dirname fixing some bugs with different file locations and systems.
0.1.2 - Removed unnecessary dependencies and fixed broken dependency.
0.1.2 - Removed unnecessary dependencies and fixed broken dependency.
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@ module.exports = serve;
* @return {Function}
* @api public
*/
function serve(root){
function serve(root, opts){
if(!root) throw Error('Root must be defined.');
if(typeof root !== 'string') throw TypeError('Path must be a defined string.');

opts = opts || {};
opts.root = process.cwd();

var rootStat = fs.statSync(root);
if(!rootStat.isDirectory()) throw Error('Root should be a directory.');

var finalFiles = walk(root);

root = fs.realpathSync(root);
if(!root) throw Error('Root must be a valid path.');

return function* staticFolder(next){
var file = finalFiles[this.path];
if(!file) return yield * next;
return yield send(this, file, {root: process.cwd()});
return yield send(this, file, opts);
}
}

Expand All @@ -46,4 +48,4 @@ function walk(directory, finalFiles) {
}
}
return finalFiles;
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-static-folder",
"version": "0.1.5",
"version": "0.1.6",
"description": "Static file server based on a folder instead of a single file. Node needs to be restarted in order to work through file caching.",
"main": "index.js",
"dependencies": {
Expand All @@ -14,7 +14,7 @@
"author": "Jan-Pieter Zoutewelle",
"license": "MIT",
"repository" :
{
{
"type" : "git",
"url" : "https://github.com/janpieterz/koajs-static-folder"
}
Expand Down

0 comments on commit e46ae6b

Please sign in to comment.