簡體   English   中英

如果我的文件是正在運行的文件,我如何使node.js執行一些代碼?

[英]How do I make node.js execute some code only if my file is the running file?

我希望能夠通過以下方式直接啟動我的快速服務器:

$ node app.js

但我也希望能夠要求該文件,讓它返回應用程序實例但實際上不啟動服務器。 然后我可以稍后用一些選項開始。

app = require './app'
app.listen options.someCustomPort

我基本上在尋找相當於這個ruby片段的東西,但在node.js中。

if __FILE__ == $0
  app.listen options[:some_custom_port]
end

這有成語嗎?

校驗

module.parent

如果它為nullundefined ,則您是主文件。 如果沒有,你一直require d。 你的module.parentrequire你的模塊的module對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM