简体   繁体   English

服务器运行时从数据库加载节点“代码”

[英]Load Node 'code' from database while server running

I'm looking to build my project in a modular fashion so that API endpoints can be added while the server is added. 我希望以模块化方式构建项目,以便可以在添加服务器时添加API端点。

Adding routes dynamically I should be able to figure out, it's getting the recently uploaded server code running that I can't figure out. 动态添加路由我应该能够弄清楚,但我无法弄清正在运行的最近上传的服务器代码。

My project has a 'class-per-endpoint' structure. 我的项目具有“按端点分类”的结构。 An endpoint has a class attached that can run code and do this and that. 端点附加了一个可以运行代码并执行此操作的类。

An endpoint can also be dependent on another endpoint/class, so what I want to be able to do is call the constructor of a dynamically added class and run the code efficiently on the server (without the API calling itself). 端点也可以依赖于另一个端点/类, 因此我想做的是调用动态添加的类的构造函数,并在服务器上有效地运行代码(无需API调用自身)。

Example where "NewAdder" endpoint was just added. 刚刚添加了“ NewAdder”端点的示例。 Rudimentary stuff, but I just hope it's clear what I'm trying to achieve. 初级的东西,但是我只是希望很清楚我要实现的目标。 Basically trying to add to the server's code base dynamically. 基本上是尝试动态添加到服务器的代码库中。

modifier.ts 修饰符

class Modifier {
    constructor(initiatedBy) {
        this.initBy = initiatedBy;
        this.modifierValue = db.getValue("modifier", {user = this.initBy})
    }
    function modify(toModify) {
        return toModify * this.modifierValue
    }
}

newAdder.ts newAdder.ts

class NewAdder {
    constructor(initiatedBy) {
         this.initBy = initiatedBy;
    }
    modifier = new Modifier(this.initBy);
    function addAndModify(a,b) {
        return modifier.modify(a + b)
    }
}

router.ts (this would be dynamic in real life) router.ts(在现实生活中将是动态的)

app.get('/newadder/addandmodify/', function(req, res){
    adder = new NewAdder(req.params.user);
    res.send(adder.addAndModify(req.params.first, req.params.second);
});

Hope I made some sense. 希望我有道理。

After some more research I suppose I could use http to get and then require the module dynamically. 经过更多研究之后,我想我可以使用http来获取,然后动态地要求该模块。

The code will come from our own server, so it should be safe to run in the server. 该代码将来自我们自己的服务器,因此在服务器上运行应该是安全的。

But if anyone has any idea how to go about this, would be much appreciated. 但是,如果有人对如何解决这个问题有任何想法,将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM