nodeJs開發app.js解析

來源:互聯網
上載者:User

標籤:

在 node.js 中模組分為核心模組和檔案模組兩種,核心模組是通過 require(‘xxxx‘) 匯入的,檔案模組是以 require(‘/xxxx‘) 或 require(‘./xxxx‘)、require(‘../xxxx‘) 形式匯入的;核心模組是用c/c++編譯的二進位模組,而檔案模組是尾碼為.js、.json、.node 的檔案,在 node.js 中一個檔案/檔案夾也可以稱之為一個模組。更多關於模組及模組載入順序的資訊請查閱官網:http://nodejs.org/api/all.html#all_modules 
這裡匯入了 express、http、path 核心模組,routes 檔案夾下的 index.js 和 user.js 檔案模組。

因為 express 架構是依賴 connect 架構(Node的一個中介軟體架構)建立而成的,可查閱 connect 文檔:http://www.senchalabs.org/connect/和 express 官方文檔:http://expressjs.com/api.html瞭解更多內容。


app.set(name, value):設定 name 的值為 value 


app.set(‘port‘, process.env.PORT || 3000):設定連接埠為 process.env.PORT 或 3000 


app.set(‘views‘, __dirname + ‘/views‘):設定 views 檔案夾為視圖檔案的目錄,存放模板檔案,__dirname 為全域變數,儲存著當前正在執行指令碼所在的目錄名。


app.set(‘view engine‘, ‘ejs‘):設定視圖模版引擎為 ejs

 

app.use([path], function):使用中介軟體 function,選擇性參數path預設為"/"


app.use(express.favicon()):connect 內建的中介軟體,使用預設的 favicon 表徵圖,如果想使用自己的表徵圖,需改為app.use(express.favicon(__dirname + ‘/public/images/favicon.ico‘)); 這裡我們把自訂的 favicon.ico 放到了 public/images 檔案夾下。


app.use(express.logger(‘dev‘)):connect 內建的中介軟體,在開發環境下使用,在終端顯示簡單的不同顏色的日誌,比如在啟動 app.js 後訪問 localhost:3000,終端會輸出:

Express server listening on port 3000 GET / 200 21ms - 206b GET /stylesheets/style.css 304 4ms 

數字200顯示為綠色,304顯示為藍色。假如你去掉這一行代碼,不管你怎麼重新整理網頁,終端都只有一行 Express server listening on port 3000。


app.use(express.bodyParser()):connect 內建的中介軟體,用來解析請求體,支援 application/json, application/x-www-form-urlencoded, 和 multipart/form-data。


app.use(express.methodOverride()):connect 內建的中介軟體,可以協助處理 POST 請求,偽裝 PUT、DELETE 和其他 HTTP 方法。


app.use(app.router):設定應用的路由(可選),詳細請參考:http://stackoverflow.com/questions/12695591/node-js-express-js-how-does-app-router-work 


app.use(express.static(path.join(__dirname, ‘public‘))):connect 內建的中介軟體,設定根目錄下的 public 檔案夾為靜態檔案伺服器,存放 image、css、js 檔案於此。


if (‘development‘ == app.get(‘env‘)) {app.use(express.errorHandler());}:開發環境下的錯誤處理,輸出錯誤資訊。

 

app.get(‘/‘, routes.index):路由控制器,如果使用者訪問" / "路徑,則由 routes.index 來控制,routes/index.js 內容如下:

exports.index = function(req, res){ res.render(‘index‘, { title: ‘Express‘ }); }; 

通過 exports.index 匯出 index 函數介面,app.get(‘/‘, routes.index) 相當於:

app.get(‘/‘, function(req, res){ res.render(‘index‘, { title: ‘Express‘ }); };) 

 

res.render(‘index‘, { title: ‘Express‘ }):調用 ejs 模板引擎解析 views/index.ejs(我們之前通過 app.set(‘views‘, __dirname + ‘/views‘)設定了模版檔案預設儲存在 views 下),並傳入一個對象作為參數,這個對象只有一個屬性 title: ‘Express‘,即用字串 Express 替換 views/index.ejs 中所有 title 變數,後面我們將會瞭解更多關於模板引的內容。

http.createServer(app).listen(app.get(‘port‘), function(){ console.log(‘Express server listening on port ‘ + app.get(‘port‘)); }); 

這段代碼的意思是建立伺服器並監聽3000連接埠,成功後在命令列中顯示 Express server listening on port 3000,然後我們就可以通過在瀏覽器輸入 localhost:3000 來訪問了。

nodeJs開發app.js解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.