Recording express.js application. Where do helper methods go?

So, I started with express.js - my first JS web design. I did not do anything small, but started a major project. I study and build at the same time.

Based on the Python / Flask background, the expression seems very complex.

As in python, if I need a helper method, I can just put it on top of the file or in a new module and import it. Super lightweight. But in node / express, everything happens asynchronously, and everything is in exportsor module.exports(??). Where do helper methods go? How do I call them using callbacks?

In another question, I asked: I did the same calculation several times. In Python, I would write a method (with instructions and if parameters) and call it several times using a loop for.. in.... The code that I have now is very redundant.

How to do it in express? What are the best methods for writing express code?

+4
source share
2 answers

It really depends on what your assistant does. If it works with data that is passed as a parameter for it, you can save it in an external module and use it requireto access it.

// helpers/FormatString.js
module.exports = function(str) {
   return str.toUpperCase();
}

// app.js
var formatter = require("./helpers/FormatString");

, request response, . :.

app.use(function(req, res, next) {
   // ... do your stuff here
});
+7

@ . , , ( foo/bar, ):

var cached; // as modules can act like singletons I can share values here

module.exports = function fetchGoogleCerts(options, callback) { // <-- callback
    var now = Date.now();
    if (ttl > now && cached) {
        callback(null, cached); // <-- return with success
        return;
    }

    request({
            uri: options.certsUrl || defaultCertsUrl,
            strictSSL: true
        }, function (err, response, body) {
            var error, certs;

            if (!err && response.statusCode === 200) {
                certs = jsonParse(body); // a local function
                if (!certs) {
                    callback('parse_error', null); // <-- return an error
                    return;
                }
                cached = certs;

                // ... more code

                callback(null, cached); // <-- success case
            } else {
                error = {
                    error: err,
                    statusCode: response.statusCode
                };
                log.error(error, 'error fetching google certs');
                callback(error); // <-- return an error
            }
        });
    }
};

:

fetchGoogleCerts = require('./google-certs.js'),


module.exports = function fetchCertsAndDecodeIdToken(token, options, callback) {
    fetchGoogleCerts(options, function (err, googleCerts) {
        if (err) {
            callback({
                errorCode: codes.io_error,
                errorMsg: 'Unable to fetch Google certificates',
                error: err
            });
            return;
        }
        decodeAndVerifyGoogleIdToken(googleCerts, token, options, callback);
    });
};

, callback .

, , EventEmitter, , . , .

+2

All Articles