Node.js command line scripts to configure the application

file: /config/index.js;

var config = {
    local: {
        mode: 'local',
        port: 3000
    },
    staging: {
        mode: 'staging',
        port: 4000
    },
    production: {
        mode: 'production',
        port: 5000
    }
}
   module.exports = function(mode) {
        return config[mode || process.argv[2] || 'local'] || config.local;
    }

file: app.js;

...
var config = require('./config')();
...
http.createServer(app).listen(config.port, function(){
    console.log('Express server listening on port ' + config.port);
});

what is the point config[mode || process.argv[2] || 'local'] || config.local;.

what I know
1) ||means "or." 2) when we enter the terminal node app.js staging, it process.argv[2]receives 2.argument from the NODE.JS command line, so this is the "stage".

please can anyone explain these code snippets?

+4
source share
1 answer

The first part defines the configuration object. Then it exports this object.

/, mode . , , :

var config = require('/config/index.js')('staging');

, / 'staging' mode, , return config.staging;, config ['staging'], .

|| . , . , mode - undefined, - process.argv[2], , , . $ node index staging. , .

2 , local ! : , , local, , config.local. , . redudant, or

+2

All Articles