【发布时间】:2014-06-24 15:24:11
【问题描述】:
我来自 PHP/Laravel 背景,但我的团队非常倾向于在我们的下一个项目中使用 Node.js(和sails),这是一个为学者提供的协作工作室。但是,在我尝试之前,我有一个关于为 node.js 应用程序创建 laravel 风格的服务提供者(或 wordpress 风格的插件)的最佳实践的问题。
在 laravel 4.1 中加载服务提供者时,类在某个点加载,调用 boot() 方法,提供者可以“挂钩”到运行时触发的事件。服务提供者列表是从配置文件中的数组中提取的(尽管从数据库中获取它们与使用 wordpress 一样简单)。
基本上,我正在寻找的是与此 PHP 代码等效的 Node.js:
// Get an array of installed plugins from a config file (or database)
$exts = require_once 'config/installed_plugins.php';
/*
* The $exts array looks like this $ext['class_name'] = '/plugin/file/path.php'
* and each plugin file is a class with a boot() method that registers events
* with callbacks
*/
// Iterate and boot each plugin
foreach ($ext as $class=> $path) {
require $path;
$c = new $class();
$c->boot(); // Now this plugins events have been registered
}
很明显,这个伪代码有很多安全漏洞,而且不适合生产,但我认为这说明了我的观点。
我知道(并且喜欢)Javascript 的事件驱动特性,所以我想使用 Node.js 很容易实现类似的东西,但我找不到在哪里或如何加载我的服务提供者并允许它们挂钩执行。
【问题讨论】:
标签: javascript node.js events laravel sails.js