如何用 node.js 做 AOP? [英] How to do AOP with node.js?
问题描述
我在用 node.js 做一些 AOP 时遇到了一个小问题:假设我在名为 server.js 的脚本中有一个应用程序,我想监视它的功能.
I have a little problem doing some AOP with node.js: Let's say I have an application in a script called server.js, and I want to monitor its functions.
代码如下:
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,但我能找到的所有东西 - 我认为这是由于 Javascript 语言的性质 - 意味着很多代码入侵.
As you can see, I want to monitor the unique function login(req, res). In order to do this, I want to use AOP within another script, but all I can find - and I think it is due to the nature of the Javascript language - implies a lot of code intrusion.
有没有办法像在 Spring/Java 中那样做 AOP?无需进行任何代码入侵?
Is there any way to do AOP just like in Spring/Java? Without having to do any code intrusion?
目前,我的解决方案是:
这是我们的应用程序,有一些代码入侵
Currently, my solution is this one:
Here is our application with some code-intrusion
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
});
最后,我所要做的就是
And finally, all I have to do is
node aop.js
它可以工作,但正如您所看到的,存在一些代码入侵.我想摆脱它.有人知道吗?
It works, but as you can see, there is some code intrusion. And I want to get rid of it. Does anyone have any idea?
推荐答案
我认为这是由于 Javascript 语言的性质 - 意味着大量代码入侵.
I think it is due to the nature of the Javascript language - implies a lot of code intrusion.
请不要:'(
AOP是OOP的扩展,没有OOP就没有AOP.
我建议您使用 kaop 或带有 ES7 装饰器的 TS 版本 kaop-ts
I suggest you to use kaop or TS version with ES7 decorators kaop-ts
1º: npm install kaop --save
2º:定义一个建议来监控你的方法:
2º: define an advice to monitor your methods:
import { reflect } from "kaop"
const Log = reflect.advice(meta => {
//meta.args contains the arguments {array}
console.log(meta.methodName + " called");
console.log("with arguments: " + meta.args);
console.log("returned: " + meta.result);
})
3º 您必须按照 OOP 准则组织代码:
3º you have to organize your code following OOP guidelines:
const Controller = createClass({
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]
})
我在 2018 年写了一篇文章,其中在JS服务器端讨论AOP.
I've writed an article on 2018 which discuss AOP on JS server side.
这篇关于如何用 node.js 做 AOP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!