3.1.4 • Published 9 years ago
engine-x v3.1.4
engine-x
EnGINe-X, nginx the node version, the reverse proxy for node.
engine-x is the subset of nginx, and only cares about the things inside the server directive of nginx, and does not care about:
listenserver_name
supported directives
- location
- rewrite
- root
- proxy_pass
Install
$ npm install engine-x --savenginx.Router
const {
Router
} = require('engine-x')
const router = new Router({
routes: [
// The matching priority of locations follows the
{
// same as `location /app {...}` directive of nginx.
location: '/app',
// `root` could be a path or an array of paths, as well as below.
root: '/path/to'
},
{
// same as `location = /app/legacy {...}` directive of nginx
location_is: '/app/legacy',
root: '/legacy/path/to'
},
{
// same as `location ~* -[a-z0-9]{7}\.png$/` directive of nginx
location: /-[a-z0-9]{7}\.png$/i,
// rewrite '/path/to/a-28dfeg0.png' -> '/path/to/a.png'
rewrite: (url, redirect) => {
return url.replace(/-[a-z0-79]{32}\.([a-z0-9]+)$/i, (m, p1) => {
return `.${p1}`
})
}
},
{
// Location could be a function
location: pathname => pathname === '/favicon.ico',
root: '/icon'
}
],
// if no location matched, then use default root
root: '/path/to/default/root',
// if no location is matched,
// and if no root specified, or no matched file found within root,
// then will proxy pass to the server
proxy_pass: 'http://domain.com'
})
router
.route({
pathname: '/app/a-28dfeg0.png'
})
.on('found', (filename) => {
filename // '/path/to/app/a.png'
})
// will not be called
.on('proxy-pass', (url) => {
})Directives
root
Could be a path or an array of paths
rewrite(url, redirect)
- url
URLurl to be rewritten - redirect
function(redirect_url, is_permanent)
nginx
rewrite {pattern} {url_rewritten} {last};- use
ifcondition of javascript to handlelastorbreakflag of nginx - the return value of
rewrite(url)is asurl_rewrittenif functionredirectnot called
nginx redirect flag
nginx
rewrite {pattern} {redirect_url} redirect;rewrite
{
rewrite: (url, redirect) => {
redirect(redirect_url)
}
}nginx permanent flag
rewrite {pattern} {redirect_url} permanent;rewrite
{
rewrite: (url, redirect) => {
// set `is_permanent` to `true`
redirect(redirect_url, true)
}
}Events
'not-found''found''error''redirect''return''proxy-pass'
License
MIT