express-static
Advanced tools
Comparing version 1.0.1 to 1.0.2
46
index.js
@@ -1,19 +0,37 @@ | ||
var fs = require('fs'); | ||
var fs = require('fs'); | ||
var url = require('url'); | ||
var path = require('path'); | ||
var mime = require('mime'); | ||
module.exports = function(root){ | ||
var Static = function(options){ | ||
this.options = options; | ||
}; | ||
Static.prototype.send = function(req, res, next){ | ||
var uri = url.parse(req.url); | ||
var pathname = uri.pathname; | ||
if(/\/$/.test(pathname)) pathname += 'index.html'; | ||
var filename = path.join(this.options.root, pathname); | ||
fs.stat(filename, function(err, stat){ | ||
if(err){ | ||
return next((~[ 'ENOENT' ].indexOf(err.code)) ? null : err); | ||
} | ||
var type = mime.lookup(filename); | ||
var charset = mime.charsets.lookup(type); | ||
res.setHeader('Content-Type' , type + (charset ? '; charset=' + charset : '' )); | ||
res.setHeader('Content-Length', stat.size); | ||
res.setHeader('Last-Modified' , stat.mtime.toUTCString()); | ||
fs.createReadStream(filename).pipe(res); | ||
}); | ||
}; | ||
module.exports = function(root, options){ | ||
options = options || {}; | ||
console.log(root); | ||
options.root = root; | ||
var static = new Static(options); | ||
return function(req, res, next){ | ||
var url = req.url; | ||
if(/\/$/.test(url)) url += 'index.html'; | ||
var filename = path.join(root, url); | ||
fs.exists(filename, function(exists){ | ||
if(exists){ | ||
fs.readFile(filename, { encoding: 'utf-8' }, function(err, data){ | ||
res.send(data); | ||
}); | ||
}else{ | ||
next(); | ||
} | ||
}); | ||
static.send.apply(static, arguments) | ||
}; | ||
}; |
{ | ||
"name": "express-static", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "", | ||
@@ -25,3 +25,6 @@ "main": "index.js", | ||
"express": "^4.9.6" | ||
}, | ||
"dependencies": { | ||
"mime": "*" | ||
} | ||
} |
var express = require('express'); | ||
var logger = require('express-log'); | ||
var static = require('../index'); | ||
@@ -6,2 +7,3 @@ | ||
app.use(logger()); | ||
app.use(static(__dirname + '/public')); | ||
@@ -8,0 +10,0 @@ |
Floating dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
3889
42
1
1
+ Addedmime@*
+ Addedmime@4.0.3(transitive)