Node.js - 运行未被引用的模块
在Node里,你可以让你的程序根据其运行自require('./something.js')
或者node something.js
而做不同的处理。如果你想与你的一个独立的模块进行交互,这是非常有用的。
if (!module.parent) {
// 通过 `node something.js` 启动
app.listen(8088, function() {
console.log('app listening on port 8088');
})
} else {
// 通过 `require('/.something.js')` 被引用
module.exports = app;
}
更多内容请看 modules的文档
MEET THE NEW JSTIPS BOOK
You no longer need 10+ years of experience to get your dream job.
Use the 100 answers in this short book to boost your confidence and skills to ace the interviews at your favorite companies like Twitter, Google and Netflix.
GET THE BOOK NOW
MEET THE NEW JSTIPS BOOK
The book to ace the JavaScript Interview.
A short book with 100 answers designed to boost your knowledge and help you ace the technical interview within a few days.
GET THE BOOK NOW