NodeJS的模組寫法入門

2021-09-01 03:18:38 字數 577 閱讀 1516

前面 提到了nodejs的命令列和web版之「hello,world」。命令列是直接執行hello.js檔案,web版的需要require http模組。http模組是nodejs自身提供的。

我們知道每個模組對應乙個js檔案,這篇寫乙個最簡單的模組hello.js, 然後在另乙個js檔案(main.js)中require自定義的模組。

hello.js

function hello(name) 

exports.hello = hello;

main.js

var h = require('./hello');

h.hello('snandy');

約定:hello.js和main.js在同乙個目錄下,比如是node目錄

開啟命令列,進入node目錄,執行命令

node main.js
可以看到命令列輸出了:hello, snandy

注意 :

main.js中require的引數不能是"hello" ,必須在前面加上"./"。

如果想將自己的模組發布到 參考這篇文章:nodejs 模組開發及發布詳解 

nodejs模組 http模組

處理url請求 var fs require fs 主頁function home res res.write content res.end about.html 關於頁面 function about res res.write content res.end 匯出頁面處理函式 exports....

nodejs安裝 nodejs入門

nodejs開篇 前幾天看到好多關於node 的帖子沒有單獨說明node安裝的文章 特發此篇 總結一下平時在windows上nodejs的安裝。1 js來搞前後端分離是nodejs的一大特點,用js來寫後台程式 當然node的最大優點個人認為 還是 單執行緒的非同步程式設計咯 2 基於nodejs使...

nodejs中的模組

nodejs中的模組管理遵循commonjs規範。使用module.exports 可簡寫為exports 匯出模組,使用require來引入模組。例 mymodule.js var myfunction function name module.exports myfunction 也可也成exp...