andrewrk / connect-static Goto Github PK
View Code? Open in Web Editor NEWstatic file server middleware for connect. loads files once at startup and saves gzipped versions in memory
License: MIT License
static file server middleware for connect. loads files once at startup and saves gzipped versions in memory
License: MIT License
Does this support watching the filesystem for new files added to the directory and/or changes to files that are being statically served? If not, would an update be considered to add that as an option or to make it possible for the user to plug in their own code that implements it?
Hey.
So I wanted to use this to catch all requests that in production mode will be delivered thru a CDN...whilst doing so, I noticed that this module does not like symlinks. I have two kinds of symlinks:
/cdn/bootstrap-accessibility -> ../node_modules/bootstrap-accessibility-plugin/plugins/
/cdn/css/normalize.css -> ../../node_modules/normalize.css/normalize.css
Neither of those got served. I have the same setup for javascript files also. Now, here is what I had to do in order to fix it:
srvStatic({
dir: config.base+"/cdn",
aliases: [ ["/","/index.html"] ],
ignoreFile: function(fullPath) {
var basename = path.basename(fullPath);
return /^\./.test(basename)
|| /~$/.test(basename)
|| /^(.+\.php)$/.test(basename);
}
}, function(err, middleware) {
if (err) throw err;
app.use(vhost(config.BIRD3.url, CDN));
CDN.use(config.CDN.baseUrl, middleware);
// Fallback handle for symlinks. Quite the callback hell...
CDN.use(config.CDN.baseUrl, function(req, res, next){
// Preps
var rqFile = config.base+"/cdn"+req.url;
var mt = mime.types[path.extname(rqFile).substr(1)];
var rqObj = {"Content-type":mt};
log.info("Static fallback: "+rqFile+" | "+mt);
if(err) { next(); throw err; }
fs.stat(rqFile, function(err,stats){
if(err) { throw err; next(); }
if(stats.isSymbolicLink()) {
fs.readlink(rqFile, function(err, linkstr){
if(err) throw err;
log.info("--> Redirecting request \""+req.url+"\" to \""+linkstr+"\"");
var baseDir = path.dirname(rqFile);
var realFile = path.join(baseDir, linkstr);
fs.readFile(realFile, function(err, data){
if(err) throw err;
res.writeHead(200,rqObj);
res.end(data);
});
});
} else {
fs.readFile(rqFile, function(err, data){
if(err) throw err;
res.writeHead(200,rqObj);
res.end(data);
});
}
});
});
});
Why aren't symlinks working OOB?
Kind regards, Ingwie
Being a bit new to Node I can't quite get it to work.
I can see that it scans the files and in theory caches them but it seems the middleware is never called in my setup.
I'm running this in my node server.js file:
createStatic({dir: "app"},
function(err, middleware) {
app.use('/', middleware);
});
Is that correct? Or should it be in my routes? If in my routes then what do I do with my default route for "/"?
var mimeType = mime.lookup(relName);
^
TypeError: mime.lookup is not a function
I guess you have to replace require 'mime' with 'mime-types'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.