Node.js的加载模块基于动态路线 [英] Loading Node.js modules dynamically based on route

查看:160
本文介绍了Node.js的加载模块基于动态路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用的Node.js前preSS做一个项目。这里是我的目录结构:

I'm doing a project in Node.js using express. Here's my directory structure:

root
|-start.js
|-server.js
|-lib/
|    api/
|        user_getDetails.js
|        user_register.js

的lib / API / 目录中有一些与API的JS文件。我需要做的是使一种拦截系统,即每当API函数一个得到来自前preSS HTTP服务器请求,它的任何行动中根据API处理程序规定。它可能混淆,但希望你的想法。

The lib/api/ directory has a number of JS files relating to the API. What I need to do is make a sort of hooking system, that whenever one of the API functions gets requested from the express HTTP server, it does whatever action is specified in the according API handler. It's probably confusing, but hopefully you get the idea.


  1. 拉里通过POST发送请求来获取用户详细信息。

  2. 服务器看起来的lib / API 找到与该请求相关联的功能。

  3. 服务器carrys出操作和数据发送回拉里。

  1. Larry sends request via POST to get user details.
  2. Server looks in lib/api to find the function associated with that request.
  3. Server carrys out action and sends back data to Larry.

希望你能帮助我。我想它可能使用的原型来完成,不知道虽然。

Hopefully you can help me out. I was thinking it could be done using prototypes, not sure though.

谢谢!

推荐答案

如果你知道你的脚本,即你有一个初步的目录,例如 DIR ,然后你可以用工作FS ,例如:

If you know where your scripts are, i.e. you have an initial directory, for example DIR, then you can work with fs, for example:

server.js

var fs = require('fs');
var path_module = require('path');
var module_holder = {};

function LoadModules(path) {
    fs.lstat(path, function(err, stat) {
        if (stat.isDirectory()) {
            // we have a directory: do a tree walk
            fs.readdir(path, function(err, files) {
                var f, l = files.length;
                for (var i = 0; i < l; i++) {
                    f = path_module.join(path, files[i]);
                    LoadModules(f);
                }
            });
        } else {
            // we have a file: load it
            require(path)(module_holder);
        }
    });
}
var DIR = path_module.join(__dirname, 'lib', 'api');
LoadModules(DIR);

exports.module_holder = module_holder;
// the usual server stuff goes here

现在你的脚本需要遵循以下结构(因为要求(路径)(module_holder)线),例如:

Now your scripts need to follow the following structure (because of the require(path)(module_holder) line), for example:

user_getDetails.js

function handler(req, res) {
    console.log('Entered my cool script!');
}

module.exports = function(module_holder) {
    // the key in this dictionary can be whatever you want
    // just make sure it won't override other modules
    module_holder['user_getDetails'] = handler;
};

现在,处理请求的时候,你做的:

and now, when handling a request, you do:

// request is supposed to fire user_getDetails script
module_holder['user_getDetails'](req, res);

这应该载入所有的模块,以 module_holder 变量。我没有测试它,但它应该工作(除了错误处理!)。你可能想改变这种功能(例如让 module_holder 一棵树,不是一个级别的字典),但我想你一定会抓住这个想法。

This should load all your modules to module_holder variable. I didn't test it, but it should work (except for the error handling!!!). You may want to alter this function (for example make module_holder a tree, not a one level dictionary) but I think you'll grasp the idea.

此函数应该每个服务器启动加载一次(如果你需要更频繁地启动它,那么你可能处理动态服务器端的脚本,这是一个baaaaaad的想法,恕我直言)。你现在唯一需要的是导出 module_holder 对象,这样每个视图处理程序可以使用它。

This function should load once per server start (if you need to fire it more often, then you are probably dealing with dynamic server-side scripting and this is a baaaaaad idea, imho). The only thing you need now is to export module_holder object so that every view handler can use it.

这篇关于Node.js的加载模块基于动态路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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