简体   繁体   中英

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

currently developing a distributed system and crashed my code last night. My server-application has multiple modules which each established a connection to my redis db, however it would be better if there is a single connection via the main module. This, I tried to implement last night, But I can't quite figure out what exactly is the problem. I'm still quite new to this field, and already searched through pages like SitePoint, Medium, and other blogs, and also checked for my problem or derived versions of it here, but found nothing what could help me enough to settle this.

UPDATE 2016/6/24 13:18
I removed the app.use() -Statement from my code and now the server is firing up. Progress :) However, If I try to test a route , the only reply I get is Cannot <METHOD> <ROUTE> .
Looking at this question here provides me with the answer to utilize the very app.use() I just removed from the code. Is there something I'm missing?

UPDATE 2016/6/24 14:05
I implemented the statement app.use('/', account_routing.routes); , doesn't work but seems the right way to go, doesn't it? I can send requests, but there is no answer whatsoever right now.

I'm providing the code for my server application:

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 */
        });
    }
};

Thank you all very much in advance for help.

So basically after an awful lot of lost nerves, I stumbled upon this nice gem here . I implemented it this very way, so now I have:

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;

and 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 */

This way, there is a single connection established to the redis database, and also the routes are working properly now. I hope this information will help somebody.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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