如何在节点js中导出和需要模块

时间:2016-07-21 10:58:23

标签: node.js node-redis

我想用Nodejs,Express和Georedis设置MVC模式,我已将所有必需的模块包含在config/require_module.js下的单个文件中作为 -

var all_modules = {};
all_modules.express  = require('express');
all_modules.router   = all_modules.express.Router();
all_modules.redis    = require('redis');
all_modules.client   = all_modules.redis.createClient();
all_modules.georedis = require('georedis');
all_modules.fs       = require('fs');
all_modules.mysqlPool = require('./myqlConn');
all_modules.locationsController = require('./../controllers/locations');

module.exports = all_modules;

controller/locations.js中,我将此文件包含为 -

`var  allModules = require('./../config/require_module');`

我把georedis模块称为 -

exports.update_location = function(req, res) {

    var geo = allModules.georedis.initialize(allModules.client, {
        zset: 'LocationsSet',
        nativeGeo: false
    });
};

但它正在抛出异常 -

TypeError: Cannot read property 'initialize' of undefined
<br> &nbsp; &nbsp;at exports.update_location (/var/www/html/nodeRedisRestexample/controllers/locations.js:27:34)

locations.js第27行是

var geo = allModules.georedis.initialize(allModules.client, {

请帮忙。谢谢

0 个答案:

没有答案