如何使用node.js进行AOP? [英] How to do AOP with node.js?

查看:403
本文介绍了如何使用node.js进行AOP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用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,但我能找到的所有内容-我认为这是由于Java语言的性质所致-隐含了许多代码入侵.

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?

推荐答案

我认为这是由于Java语言的性质所致-暗含了 很多代码入侵.

I think it is due to the nature of the Javascript language - implies a lot of code intrusion.

请不要:'(

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

我建议您将 kaop 或TS版本与ES7装饰器

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆