Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Last active December 11, 2015 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishiduca/4517619 to your computer and use it in GitHub Desktop.
Save ishiduca/4517619 to your computer and use it in GitHub Desktop.
呼び出されるモジュール側から呼び出し(実行スクリプト)側のパスを取得する
#!/usr/bin/env node
var path = require('path');
var waf = require('w-a-f');
waf().use(require(path.join(__dirname, 'middleware/wan'))())
.use(require(path.join(__dirname, 'middleware/nyan'))())
.use(require(path.join(__dirname, 'middleware/guu'))())
.resume();
// wan
// nyan
// guu
waf().use('guu').use('nyan').use('wan').resume();
// guu
// nyan
// wan
module.exports = function () {
return funciton (next) {
console.log('guu');
next();
};
};
var path = require('path');
function WAF () {
this.middlewares = [];
}
WAF.prototype.use = function (middleware, arg) {
'string' === typeof middleware &&
(middleware = this.useHelp(middleware, arg));
'function' === typeof middleware &&
this.middlewares.push(middleware);
return this;
};
WAF.prototype.useHelp = function (middleware_name, arg) {
return require(path.join( path.dirname(process.argv[1])
, 'middleware', middleware_name ))(arg);
};
WAF.prototype.resume = function () {
var that = this, i = 0, next = function () {
var m = that.middlewares[i++];
'function' === typeof m && m(next);
};
next();
};
module.exports = function () {
return new WAF;
};
module.exports = function () {
return funciton (next) {
console.log('nyan');
next();
};
};
.
|-- app.js
|-- middleware
| |-- guu.js
| |-- nyan.js
| `-- wan.js
`-- node_modules
`-- w-a-f
|-- index.js
`-- package.json
module.exports = function () {
return funciton (next) {
console.log('wan');
next();
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment