我试图理解 lodash 中的分配/扩展函数,因为我通过 mean.io 堆栈设置遇到了一个非常令人困惑的结果。
这里:
'use strict';
var _ = require('lodash');
// Load app configuration
var foo = require(__dirname + '/../config/env/all.js');
console.log('-.-.-.-.-.-.-.-.-.-. foo before _.assign .-.-.-.-.-.-.-.-.-.-');
console.log(foo)
module.exports = _.assign(
require(__dirname + '/../config/env/all.js'),
require(__dirname + '/../config/env/' + process.env.NODE_ENV + '.js') || {}
);
var bar = require(__dirname + '/../config/env/all.js');
console.log('-.-.-.-.-.-.-.-.-.-. foo after _.assign .-.-.-.-.-.-.-.-.-.-');
console.log(foo);
console.log('-.-.-.-.-.-.-.-.-.-. bar .-.-.-.-.-.-.-.-.-.-');
console.log(bar);
console.log('-.-.-.-.-.-.-.-.-.-. A direct require call .-.-.-.-.-.-.-.-.-.-');
console.log(require(__dirname + '/../config/env/all.js'));
我得到的是:
-.-.-.-.-.-.-.-.-.-. foo before _.assign .-.-.-.-.-.-.-.-.-.-
{ root: '/home/bax/Code/lab/mean/test1',
port: 3000,
db: undefined }
-.-.-.-.-.-.-.-.-.-. foo after _.assign .-.-.-.-.-.-.-.-.-.-
{ root: '/home/bax/Code/lab/mean/test1',
port: 3000,
db: 'mongodb://localhost/mean-dev',
app: { name: 'MEAN - A Modern Stack - Development' },
facebook:
{ clientID: 'APP_ID',
clientSecret: 'APP_SECRET',
callbackURL: 'http://localhost:3000/auth/facebook/callback' },
twitter:
{ clientID: 'CONSUMER_KEY',
clientSecret: 'CONSUMER_SECRET',
callbackURL: 'http://localhost:3000/auth/twitter/callback' },
github:
{ clientID: 'APP_ID',
clientSecret: 'APP_SECRET',
callbackURL: 'http://localhost:3000/auth/github/callback' },
google:
{ clientID: 'APP_ID',
clientSecret: 'APP_SECRET',
callbackURL: 'http://localhost:3000/auth/google/callback' } }
-.-.-.-.-.-.-.-.-.-. bar .-.-.-.-.-.-.-.-.-.-
{ root: '/home/bax/Code/lab/mean/test1',
port: 3000,
db: 'mongodb://localhost/mean-dev',
app: { name: 'MEAN - A Modern Stack - Development' },
facebook:
{ clientID: 'APP_ID',
clientSecret: 'APP_SECRET',
callbackURL: 'http://localhost:3000/auth/facebook/callback' },
twitter:
{ clientID: 'CONSUMER_KEY',
clientSecret: 'CONSUMER_SECRET',
callbackURL: 'http://localhost:3000/auth/twitter/callback' },
github:
{ clientID: 'APP_ID',
clientSecret: 'APP_SECRET',
callbackURL: 'http://localhost:3000/auth/github/callback' },
google:
{ clientID: 'APP_ID',
clientSecret: 'APP_SECRET',
callbackURL: 'http://localhost:3000/auth/google/callback' } }
-.-.-.-.-.-.-.-.-.-. A direct require call .-.-.-.-.-.-.-.-.-.-
{ root: '/home/bax/Code/lab/mean/test1',
port: 3000,
db: 'mongodb://localhost/mean-dev',
app: { name: 'MEAN - A Modern Stack - Development' },
facebook:
{ clientID: 'APP_ID',
clientSecret: 'APP_SECRET',
callbackURL: 'http://localhost:3000/auth/facebook/callback' },
twitter:
{ clientID: 'CONSUMER_KEY',
clientSecret: 'CONSUMER_SECRET',
callbackURL: 'http://localhost:3000/auth/twitter/callback' },
github:
{ clientID: 'APP_ID',
clientSecret: 'APP_SECRET',
callbackURL: 'http://localhost:3000/auth/github/callback' },
google:
{ clientID: 'APP_ID',
clientSecret: 'APP_SECRET',
callbackURL: 'http://localhost:3000/auth/google/callback' } }
Express app started on port 3000
问题是:我不应该每次都得到相同的结果:console.log(require(__dirname + '/../config/env/all.js'));
?