【发布时间】:2014-01-02 03:27:24
【问题描述】:
开源博客项目 Ghost 有一个 index.js 文件,其中只有这段代码。
// # Ghost bootloader
// Orchestrates the loading of Ghost
// When run from command line.
var ghost = require('./core');
ghost();
如果您运行 node index.js,它会启动应用程序。 require语句中的./core其实是一个目录,里面有很多子目录,所以这个index.js文件本质上就是把整个目录(里面有很多函数和文件)作为函数ghost();调用,这就引出了一个问题,当ghost(); 发生时,实际上首先调用的是什么?它会自动在 /core 目录中查找 index.js 文件吗?
比如./core里面,除了一堆其他目录,还有这个index.js这个文件,里面有一个函数startGhost
// # Ghost bootloader
// Orchestrates the loading of Ghost
// When run from command line.
var config = require('./server/config'),
errors = require('./server/errorHandling');
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
function startGhost(app) {
config.load().then(function () {
var ghost = require('./server');
ghost(app);
}).otherwise(errors.logAndThrowError);
}
module.exports = startGhost;
所以我的问题是,当有这样的设置时,整个目录都像函数一样被调用
var ghost = require('./core');
ghost();
node 是否默认在 ./core 中查找 index.js 文件,并且在这种情况下调用 startGhost?
【问题讨论】:
标签: node.js