Node.js - 執行尚未被 required 的模組
在 nodejs,你可以讓你的程式取決於 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;
}
更多資訊請參考 nodejs 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