如何用node.js做AOP?

我在使用node.js做一些AOP时遇到了一些问题:假设我在一个名为server.js的脚本中有一个应用程序,并且我想监视它的function。

这里是代码:

var express = require('express'); var app = express(); app.get('/', function(req, res){ res.setHeader('Content-Type', 'text/plain'); res.end('Home'); }); app.get('/login', function(req, res){ login(req,res); module.exports.login_(req, res); }); app.use(function(req, res, next){ res.setHeader('Content-Type', 'text/plain'); res.send(404, 'Page introuvable !'); }); function login(req, res){ res.setHeader('Content-Type', 'text/plain'); res.end('Page de login'); } app.listen(1616); 

正如你所看到的,我想监视唯一的函数login(req,res) 。 为了做到这一点,我想在另一个脚本中使用AOP,但是我可以find – 我认为这是由于Javascript语言的本质 – 意味着大量的代码入侵。

有没有什么办法像Spring / Java一样来执行AOP? 不需要做任何代码入侵?

目前,我的解决scheme是这样的:
这是我们的应用程序与一些代码入侵

  var express = require('express'); var app = express(); app.get('/', function(req, res){ res.setHeader('Content-Type', 'text/plain'); res.end('Home'); }); app.get('/login', function(req, res){ //We need to use the function in module.exports //--> code intrusion //login(req,res); module.exports.login_(req, res); }); app.use(function(req, res, next){ res.setHeader('Content-Type', 'text/plain'); res.send(404, 'Page introuvable !'); }); function login(req, res){ res.setHeader('Content-Type', 'text/plain'); res.end('Page de login'); } //We wrap here the function we want to monitor wrappedLogin = function(req, res){ login(req, res); } module.exports = { login_ : wrappedLogin }; app.listen(1616); 

这是我们的AOP脚本

 var aop = require("node-aop"); //Include the server var server = require('./server.js'); aop.before(server, "login_", function(key, value){ //I do some stuff here }); aop.after(server, "login_", function(key, value){ //I do some stuff here }); 

最后,我所要做的就是

 node aop.js 

它工作,但正如你所看到的,有一些代码入侵。 我想摆脱它。 有人有什么主意吗?

一个天真的尝试会看起来像这样。

 var before = function(object, functionName, action) { var oldFunction = object.functionName; var newFunction = function() { action(); oldFunction(); }; object.functionName = oldFunction; } var after = function(object, functionName, action) { var fn = object.functionName; var after = function() { fn(); action(); }; object.functionName = oldFunction; } 

JS非常灵活; 你可以通过存储之前和之前的动作并根据需要添加/删除它们来轻松地改进,但至less这应该做你想做的事情(虽然不是很好)。

我认为这是由于Javascript语言的本质 – 意味着大量的代码入侵。

请不要:'(

AOP是OOP的扩展,没有OOP就没有AOP。

我build议你使用kaop或TS版本与ES7装饰kaop ts

1º: npm install kaop --save

2º:定义一个build议来监控你的方法:

 var Advices = require("kaop").Advices; Advices.add( function log(){ //meta.args contains the arguments {array} console.log(meta.methodName + " called"); console.log("with arguments: " + meta.args); console.log("returned: " + meta.result); } ) 

3º你必须按照OOP的指导来组织你的代码:

 Controller = Class({ constructor: function(app){ app.get('/', this.home); app.get('/login', this.login); app.use(this.notFound); }, login: [function(req, res){ //what ever }, "log"], home: [function(req, res){ res.setHeader('Content-Type', 'text/plain'); res.end('Home'); }, "log"], notFound: [function(req, res){ res.setHeader('Content-Type', 'text/plain'); res.send(404, 'Page introuvable !'); }, "log"] }) 

这里是一个不使用快递的例子

我也写了一篇关于JavaScript的AOP的文章

https://medium.com/@k1r0s/aspect-oriented-programming-in-javascript-es5-typescript-d751dda576d0#.3d04ziock