$ cnpm install node-static-alias
Serve static file which is not requested file. (e.g. file.min.js is requested, serve file.js)
node-static-alias wraps (inherits) the useful module node-static, and this adds the alias option to that.
This works like the Alias mapping or the mod_rewrite
of Apache. It looks like DirectoryIndex too. And this can check the file exists or not.
file.js instead of file.min.js which is not made yet, in the test phase.index.html when */ is requested.var staticAlias = require('node-static-alias');
// Document-Root: './public' directory
var fileServer = new staticAlias.Server('./public', {
alias: {
match: '/path/to/file.min.js',
serve: '/path/to/file.js'
}
});
require('http').createServer(function(request, response) {
request.addListener('end', function() {
fileServer.serve(request, response);
}).resume();
}).listen(8080);
The alias option is added to the node-static via using require('node-static-alias') instead of require('node-static'). See node-static to use it.
aliasThe alias included in the constructor options is an Alias-Rule Object, or an array which includes multiple Alias-Rule Objects.
alias: {
match: /file\.min\.(?:js|css)$/,
serve: '/path/to/file.<% suffix %>'
}
Or
alias: [
{
match: '/path/to/file.min.js',
serve: '/path/to/file.js'
},
{
match: 'suffix=png',
serve: '../outer/<% fileName %>',
allowOutside: true
}
]
The Alias-Rule Object can have following properties.
matchType: string, RegExp, function or Array
Specify one of below or an Array which includes multiple things of those.
If one or more things match, the serve is parsed. If anything doesn't match, it goes to next an Alias-Rule. If all Alias-Rules don't match, it tries to serve the requested file.
parameter=value format (e.g. suffix=png). See Parameters. If the value is equal to the specified parameter, it's matched. alias: [
{
match: '/path/to/file.min.js',
serve: '/path/to/file.js'
},
// Image files are not made yet.
{
match: 'suffix=png',
serve: '/path/to/dummy.png'
}
]
// These image files are not made yet.
alias: {
match: /\/(?:foo|bar)\.png$/,
serve: '/path/to/dummy.png'
}
true or false.
The Object which has parameters is passed to this function. See Parameters. Also, current request instance and response instance are passed.this refers to own instance. // Kick direct access from outside web site.
alias: {
match: function(params, request, response) {
console.log(request.url + ' was requested, in the path ' + this.root);
return params.suffix === 'jpg' &&
params.referer.indexOf('http://mysite.com/') !== 0;
},
serve: '/path/to/denial.jpg'
}
serveType: string, function or Array
Specify one of below or an Array which includes multiple things of those.
By default, the first file which exists is chosen to try to serve. See force. If anything doesn't exist, it goes to next an Alias-Rule. If all files of Alias-Rules don't exist, it tries to serve the requested file.
<% parameter %>. See Parameters. // directoryIndex isn't index.html
alias: {
match: /\/$/,
serve: '<% absPath %>/default.html'
}
NOTE: If the first character of this string is / (it might be parameter), this string is absolute path. This / doesn't point the document-root. It is the root of the local filesystem. If you want the relative path from the document-root, don't specify leading /, or add . to left of leading /.
request instance and response instance are passed.this refers to own instance. // Minified files are not made yet.
alias: {
match: /\.min\.(?:js|css)$/,
serve: function(params, request, response) {
response.setHeader('X-serve-from', this.root);
return params.absDir + '/' +
params.basename.replace(/\.min$/, '.') + params.suffix;
}
}
// Compile unwatched SASS now.
alias: {
match: 'suffix=css',
serve: function(params) {
require('exec-sync')('sass ' +
params.absDir + '/' + params.basename + '.scss:' + params.absPath);
return params.absPath;
}
}
forceType: boolean
If true is specified, the first file in the serve is chosen to try to serve without checking it's existing or not. And if it doesn't exist, a 404 error occurs. Default is false.
This is used to prevent another file from being chosen unintentionally.
allowOutsideIf true is specified, serving the outside files of the document-root is allowed. Default is false.
// Shared files.
alias: {
match: /^\/common_lib/,
serve: '/path/to/lib/<% fileName %>',
allowOutside: true
}
NOTE: If you specify true in the public server, you should specify the absolute path to the serve. Otherwise the user might access to the file that must be hidden from them.
The string parameter=value can be specified to the match, and the string <% parameter %> can be specified to the serve.
And also, the Object which has parameters is passed to function which specified to the match and the serve.
These parameters are below.
reqUrl/path/to/file.ext?key1=value1&key2=value2reqPath/path/to/file.ext/reqDirreqPath. e.g. /path/toabsPath/var/www/public/path/to/file.extabsDirabsPath. e.g. /var/www/public/path/tofileNamefile.exttobasename. isn't included) e.g. filesuffix. isn't included) e.g. extreqQueryreqUrl. e.g. key1=value1&key2=value2query_ and a value. e.g. query_key1: value1, query_key2: value2 from reqQuery above[INDEX]. e.g. query_key[0]: value1, query_key[1]: value2 from key=value1&key=value2referer, user-agent, etc.The logger included in constructor options is a Logger instance of the standard Logging Library (e.g. log4js) which has info method or log method.
var log4js = require('log4js');
var logger = log4js.getLogger('node-static-alias');
logger.setLevel(log4js.levels.INFO);
var fileServer = new staticAlias.Server('./public' {
alias: { ... },
logger: logger
});
You can specify a simple Object which has info method or log method (e.g. console or util).
Most easy:
var fileServer = new staticAlias.Server('./public' {
alias: { ... },
logger: console
//logger: require('util') // Add timestamp
});
Add project name: (But, you probably use your favorite library.)
var fileServer = new staticAlias.Server('./public' {
alias: { ... },
logger: {log: function() {
var util = require('util');
console.log('[node-static-alias] ' + util.format.apply(util, arguments));
}}
});
Log message example:
(/) Requested: "/var/public"
(/file.min.css) Requested: "/var/public/file.min.css"
(/file.min.css) For Serve: "/var/public/file.css" alias[3] match[1] serve[0]
(/file.min.js) Requested: "/var/public/file.min.js"
(/file.min.js) For Serve: "/var/public/file.js" alias[2] match[0] serve[1]
The (path) is the path which is requested by the user. The [number] means an index of an Array.
This example generates a list of files and directories in requested directory when the user accessed the directory. This works like the mod_autoindex of Apache.
That looks like:
The statsFilelist is required.
npm install stats-filelist
Load that and some core modules.
var filelist = require('stats-filelist'),
path = require('path'),
fs = require('fs');
Specify for alias:
alias: {
match: function(params) {
try {
return fs.statSync(params.absPath).isDirectory();
} catch (error) {
return false; // Ignore "Not found" etc.
}
},
serve: function(params) {
var indexPath = path.join(params.absPath, '.index.html');
fs.writeFileSync(indexPath,
'<html><body><ul>' +
filelist.getSync(params.absPath, /^(?!.*[/\\]\.index\.html$).+$/, false)
.map(function(stat) {
var relPath = stat.name + (stat.isDirectory() ? '/' : '');
return '<li><a href="' + relPath + '">' + relPath + '</a></li>';
}).join('') +
'</ul></body></html>');
return indexPath;
}
}
Copyright 2013 - present © cnpmjs.org | Home |