1. 程式人生 > >Node.js-require的使用方法

Node.js-require的使用方法

require('http') 內建模組


require('./server')  “./”表示當前路徑,後面跟的是相對路徑
require("../lib/server") ../表示上一級目錄,後面跟的也是相對路徑




server.js


[javascript] 
var http = require('http'); 
function start(){ 
    server = http.createServer(function (req, res) {   
          res.writeHeader(200, {"Content-Type": "text/plain"});   
          res.end("Hello oschina\n");   
    })   
    server.listen(8000);   
    console.log("httpd start @8000");  

exports.start = start;  


index.js


[javascript] 
//路徑根據自己的實際情況而定 
var server = require("./learnNode/server"); 
server.start(); 


下面介紹require的只是來自於連結:http://www.nodecn.org/modules.html#file_Modules


模組
Node 使用 CommonJS 模組系統。


Node 有一個簡單的模組載入系統。在 Node 中,檔案和模組一一對應。比如,在 foo.js 載入同一目錄中的circle.js 模組。


foo.js 的內容:


var circle = require('./circle.js');
console.log( 'The area of a circle of radius 4 is '
           + circle.area(4));
circle.js 的內容:


var PI = Math.PI;


exports.area = function (r) {
  return PI * r * r;
};


exports.circumference = function (r) {
  return 2 * PI * r;
};
模組 circle.js 匯出了 area() 函式和 circumference() 函式,這樣它們就能從模組外部訪問了。要匯出物件,將其新增到特殊的 exports 物件就行。


模組的區域性變數是私有的。在本例中,變數 PI 是 circle.js 私有的。


核心模組
Node 有一些已編譯成二進位制的模組,這些模組將在本文件的其他地方詳細​​介紹。


核心模組在 Node 原始碼的 lib/ 資料夾中定義。


使用 require() 時,核心模組總是優先載入。例如,require('http') 總是返回內建的 HTTP 模組,即使該名稱的檔案存在。


檔案模組
如果沒有找到確切的檔案,Node 將嘗試給所需的檔名​​新增 .js 字尾再載入,然後再嘗試 .node。


.js 檔案被視為 JavaScript 文字檔案,而 .node 檔案被視為已編譯的外掛模組,用 dlopen 載入。


模組以 '/' 開頭表示使用檔案的絕對路徑。例如,require('/home/marco/foo.js') 將載入/home/marco/foo.js 檔案。


模組以 './' 開頭表示呼叫 require() 時使用相對路徑。也就是說,為了保證 require('./circle') 能找到,circle.js 必須和 foo.js 在同一目錄。


如果不以 '/' 或'./' 開頭,該模組可以是一個“核心模組”,也可是一個從 node_modules 資料夾中載入的模組。


從 `node_modules` 資料夾中載入
如果傳遞給 require() 有模組識別符號是不是原生模組,而且不以 '/'、'../' 或'./' 開頭,那麼 Node 從當前模組的父目錄+/node_modules 這個位置嘗試載入。


如果還是沒有找到,那麼它跳到上層目錄並依此類推,直到找到模組,或者達到根目錄為止。


例如,如果在檔案 '/home/ry/projects/foo.js' 中呼叫 require('bar.js'),那麼 Node 將在下列位置查詢,順序如下:


/home/ry/projects/node_modules/bar.js
/home/ry/node_modules/bar.js
/home/node_modules/bar.js
/node_modules/bar.js
這就允許程式將依賴關係本地化,防止它們衝突。


優化 `node_modules` 查詢過程


當巢狀依賴關係的層次很深時,這個檔案查詢列表可能會變得很長。因此,在查詢時進行如下優化:


首先,/node_modules 不會附加到一個以 /node_modules 結尾的資料夾後面。


其次,如果呼叫 require() 的檔案已經在一個 node_modules 層級裡,那麼最頂層的 node_modules 資料夾將被視為搜尋樹的根。


例如,如果在檔案 '/home/ry/projects/foo/node_modules/bar/node_modules/baz/quux.js' 中呼叫require('asdf.js'),那麼 Node 將搜尋下列位置:


/home/ry/projects/foo/node_modules/bar/node_modules/baz/node_modules/asdf.js
/home/ry/projects/foo/node_modules/bar/node_modules/asdf.js
/home/ry/projects/foo/node_modules/asdf.js
以資料夾作為模組
Node 允許使用者在獨立的目錄中方便地組織程式,然後提供單一入口指向該庫。有三種方式可以將資料夾作為require() 的引數。


第一種方式是在該資料夾中建立 package.json 檔案,指定一個 main 模組。一個典型的 package.json 檔案可能看起來像這樣:


{ "name" : "some-library",
  "main" : "./lib/some-library.js" }
如果此檔案位於 ./some-library 資料夾,則 require('./some-library') 會嘗試載入 ./some-library/lib/some-library.js。


這是 Node 能找到 package.json 檔案的情況。


如果在該目錄中沒有 package.json 檔案,那麼 Node 將嘗試載入該目錄中的 index.js 或 index.node 檔案。例如,如果上面的例子找不到 package.json,那麼 require('./some-library') 將試圖載入:


./some-library/index.js
./some-library/index.node
快取