Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed for Koajs (v1 & v2) #169

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions middleware/koa/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.serveStatic = require('./serveStatic');
47 changes: 25 additions & 22 deletions middleware/koa/serveStatic.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require('raptor-polyfill/string/startsWith');
require('raptor-polyfill/string/endsWith');

var lasso = require('../');
var lasso = require('../../');
var send = require('send');
var extend = require('raptor-util/extend');

function notFound() {
this.error(404);
}

module.exports = function(options) {
module.exports = function (options) {
options = options || {};

var myLasso = options.lasso || lasso.getDefaultLasso();
Expand All @@ -23,7 +23,7 @@ module.exports = function(options) {
}

if (!outputDir || !urlPrefix) {
return function(req, res, next) {
return function (req, res, next) {
return next();
};
}
Expand All @@ -40,31 +40,34 @@ module.exports = function(options) {

sendOptions.root = outputDir;

return function* (next) {

return function(ctx, next) {
var req = ctx.request,
res = ctx.response;
var ctx = this;

var path = req.path;
if (!path.startsWith(routePrefix) || (req.method !== 'GET' && req.method !== 'HEAD')) {
return next();
var path = ctx.request.path;
if (!path.startsWith(routePrefix) || (ctx.request.method !== 'GET' && ctx.request.method !== 'HEAD')) {
return yield next;
}

var filePath = path.substring(routePrefix.length);

// create send stream
var stream = send(req, filePath, sendOptions);
ctx.respond = false;

// add directory handler
stream.on('directory', notFound);
var filePath = path.substring(routePrefix.length);

// forward errors
stream.on('error', function error(err) {
res.statusCode = err.statusCode || 500;
res.end('Not found: ' + filePath);
yield new Promise(function (resolve, reject) {
send(ctx.req, filePath, sendOptions)
.on('error', function(err) {
ctx.res.statusCode = err.statusCode || 500;
ctx.res.end('Not found: ' + filePath);
reject(err);
})
.on('directory', notFound)
.on('headers', function(req, path, stat) {
ctx.status = 200;
})
.on('end', function() {
resolve();
})
.pipe(ctx.res);
});

// pipe
stream.pipe(res);
};
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
},
"author": "Patrick Steele-Idem <[email protected]>",
"maintainers": "Patrick Steele-Idem <[email protected]>",
"contributors": [
"Karl Morrison <[email protected]>"
],
"dependencies": {
"app-module-path": "^1.0.0",
"app-root-dir": "^1.0.0",
Expand Down Expand Up @@ -68,5 +71,5 @@
"concat",
"minify"
],
"version": "2.6.0"
"version": "2.6.1"
}