iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 2
0
自我挑戰組

一點點理解NodeJs系列系列 第 2

NodeJs的基本配備(1) -- 模組

  • 分享至 

  • xImage
  •  

前言

NodeJs是基於CommonJS的規範實現模組系統的,而模組化為NodeJs重要一環。

CommonJs模組定義

1. 模組引用(require)

=>獲取模塊的模塊公開的接口(exports)對象

引入情況

  • http、fs、path等,原生模塊。
    • 範例: let example = require('http')
  • ./example(.js)或../example(.js),相對路徑的文件模塊。
    • 範例: let example = require('./example(.js)')let example = require('../example(.js)')
  • /pathmodule/example(.js),絕對路徑的文件模塊<以'/'為開頭>。
    • 範例: let example = require('/pathmodule/example(.js)')
  • example,非原生模塊的文件模塊<像是/node_modules/example>。
    • 範例: let example = require('example')

2. 模組定義(exports)

=>模塊公開的接口

輸出情況

  • exports.example = function () { console . log ( 'Hello Example' ); }

  • function Example () { let name ; this.setName = function ( thyName ) { name = thyName ; } this.sayHello = function () { console.log ( 'Hello ' + name ); }; } ; module.exports = Hello ;

3. 模組標識(module)

先不介紹

結語

今天先部分了解node配備-模組,明天再繼續一點點理解NodeJs吧!


上一篇
NodeJs你是誰?
系列文
一點點理解NodeJs系列2
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言