繁体   English   中英

Node.js,Express,Redis - 无法调用来自单独模块的路由

[英]Node.js, Express, Redis - Routes from separate modules cannot be called

目前正在开发一个分布式系统,昨晚我的代码崩溃了。 我的服务器应用程序有多个模块,每个模块都建立了与我的redis db的连接,但是如果通过主模块有一个连接则会更好。 这个,我昨晚试图实施,但我无法弄清楚到底是什么问题。 我对这个领域还很陌生,已经搜索了SitePoint,Medium和其他博客等网页,并在这里检查了我的问题或衍生版本,但没有找到任何可以帮助我解决这个问题的方法。

更新2016/6/24 13:18
我从我的代码中删除了app.use() Statement,现在服务器正在启动。 进展:)但是,如果我尝试测试路线,我得到的唯一回复是Cannot <METHOD> <ROUTE>
这里查看这个问题为我提供了使用我刚从代码中删除的app.use()的答案。 有什么我想念的吗?

更新2016/6/24 14:05
我实现了语句app.use('/', account_routing.routes); ,不起作用但似乎是正确的方法,不是吗? 我可以发送请求,但现在没有任何答案。

我正在为我的服务器应用程序提供代码:

main.js

var
    express         = require('express'),
    redis           = require('redis'),

    app             = express(),
    port            = process.env.port || 3000,

    rclient         = redis.createClient(),

    account_routing = require('./modules/account_routing');

// ### CONFIGURATION ################################################

rclient.on('connect', function (err, res) {
    console.log(err || "Connected to database!");
});

account_routing.setClient(rclient);

app.use('/', account_routing.routes);

// ### FIREUP #######################################################
app.listen(port, function () {
    console.log('Server available at port ' + port);
});

account_routing.js

var rclient, express;

express = require('express');

module.exports = {

    setClient : function (inClient) { 'use strict'; rclient = inClient; },

    routes : function(app) {

// ### VARIABLES ####################################################    

        var bodyParser, http, morgan, redis, rclient, app;

// ### DEPENDENCIES #################################################

        bodyParser  = require('body-parser');
        http        = require('http');
        morgan      = require('morgan');
        redis       = require('redis');
        app         = express();

// ### CONFIGURATION ################################################

        app.use(morgan('dev'));
        app.use(bodyParser.json());
        app.use(bodyParser.urlencoded({ extended: false }));

// ### ROUTES #######################################################   

        app.get('/users/:username', function (req, res) {
            /* Access database via connection in main module
               to retrieve user data */
        });
    }
};

非常感谢大家的帮助。

所以基本上在经历了大量失去的神经之后,我在这里偶然发现了这个漂亮的宝石 我这样实现了它,所以现在我有:

main.js

"use strict";
var
    express         = require('express'),
    bodyParser      = require('body-parser'),
    app             = express(),
    port            = process.env.port || 3000,

    account_routing = require('./modules/account_routing')(app);

app.listen(port, function () {
    console.log('Server available at port ' + port);
});

redis_db.js

var
    redis = require('redis'),
    rclient = redis.createClient();

rclient.on('connect', function (err, response) {
    'use strict';
    console.log("Connected to database");
});

module.exports = rclient;

和account_routing.js

'use strict';
module.exports = function (app) {

    var
        bodyParser      = require('body-parser'),
        http            = require('http'),
        morgan          = require('morgan'),
        rclient         = require('./redis_db');

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: false}));
    app.use(morgan('dev'));

    /* Routes */

这样,就可以建立与redis数据库的单一连接,并且路由现在也正常工作。 我希望这些信息对某人有所帮助。

暂无
暂无

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

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