DIV CSS 佈局教程網

Node.js 模組寫法入門:exports 與 require
編輯:關於JavaScript     

在 Node.js 中,每個檔案都可以視為一個模組。透過 exports 可以對外提供功能,再用 require 載入其他模組。

建立 hello 模組

function hello(name) {
  console.log("hello, " + name);
}

exports.hello = hello;

在主程式載入

var h = require("./hello");
h.hello("snandy");

執行 node main.js 後,命令列會輸出 hello, snandy。

需要注意的是,require 載入同目錄下的自訂模組時,路徑前方要加上 ./,例如 require("./hello"),而不是直接寫 require("hello")。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved