Nodejs的express使用教程_node.js

來源:互聯網
上載者:User

Express 是一個簡潔、靈活的 node.js Web 應用程式開發架構, 它提供一系列強大的特性,協助你建立各種 Web 和行動裝置應用。

1.express組織圖

app demo

    |---node_modules------用於安裝本地模組。
    |---public------------用於存放使用者可以下載到的檔案,比如圖片、指令碼、樣式表等。
    |---routes------------用於存放路由檔案。
    |---views-------------用於存放網頁的模板。
    |---app.js------------應用程式的啟動指令碼。
    |---package.json------項目的設定檔。

2..建立express伺服器

//app.js檔案var express = require('express');var app = express();//指定更目錄顯示的內容app.get('/', function(req, res){ res.send('Hello World');});//指定監聽連接埠var server = app.listen(3000, function() {  console.log('Listening on port %d', server.address().port);});

運行nodejs應用程式

/>node app.js

3.中介軟體

中介軟體(middleware)就是處理HTTP請求的函數.
當一個HTTP請求進入伺服器,伺服器執行個體會調用第一個中介軟體,完成後根據設定,決定是否再調用下一個中介軟體.

中介軟體的參數為:

.四個的時候---第一個為錯誤處理,第二個為客戶請求request,第三個為伺服器響應respond,第四個為next中介軟體. 如function(error, request, response, next){}
.三個的時候---第一個客戶請求request,第二個為伺服器響應respond,第三個為next中介軟體. 如function(request, response, next){}
.兩個的時候---第一個客戶請求request,第二個為伺服器響應respondfunction. 如function(request, response){}

4.使用中介軟體use

use是express調用中介軟體的方法,它返回一個函數.

app.use(function(request, response) { response.writeHead(200, { "Content-Type": "text/plain" }); response.end("Hello world!\n");});

5.錯誤內容顯示

app.use(express.bodyParser());//使用body參數app.use(express.methodOverride());//使用函數覆蓋app.use(app.router);//使用路由app.use(function(err, req, res, next){ console.error(err.stack); res.send(500, 'Something broke!');});//錯誤內容顯示

6.路由

express路由的方式有多種,這裡舉例常用的幾種:

.app.use('/', middleware);//get/post時,對於路徑/的處理
.app.get("/", middleware);//http中get時,對於路徑/的處理
.app.post("/", middleware);//http中post時,對於路徑/的處理
.app.put("/", middleware);//http中put時,對於路徑/的處理
.app.delete("/", middleware);//http中delete時,對於路徑/的處理

7.路徑萬用字元*

.*表示所有路徑

app.get("*", function(request, response) { response.end("404!");});//所有路徑都返回404

.:捕獲路徑內容

app.get("/hello/:who", function(req, res) { res.end("Hello, " + req.params.who + ".");});//如"/hello/alice”網址,網址中的alice將被捕獲,作為req.params.who屬性的值

8.設定環境變數set

set用於指定變數的值.
app.set("view engine", "ejs");//使用ejs作為模版

9.response對象方法

.重新導向redirect

    response.redirect("/hello/anime");//重新導向到/hello/anime

.傳送檔案sendFile

   response.sendFile("/path/to/anime.mp4");

.渲染網頁模板render,即把變換的內容載入到網頁.
    response.render("index", { message: "Hello World" });//將message變數傳入index模板,值為"Hello World"渲染成HTML網頁

10.requst對象方法

.擷取客戶ip地址:request.ip

.擷取上傳的檔案:request.files

11.啟動指令碼package.json

package.json用於指定app資訊,nodejs版本號碼和其他組件的依賴關係

{  "name": "demo",  "description": "My First Express App",  "version": "0.0.1",  "dependencies": {   "express": "3.x"  }}

12.app入口app.js

app.js主要包含http的建立,基本路由,監聽連接埠號碼

13.動態網頁模板views

views檔案夾,用於存放所有的放網頁模板.

//app.jsapp.get('/', function(req, res) {  res.render('index',{title:"最近文章"});});//index.jsthis is <%=title%>!

14.指定靜態網頁目錄

//app.js
app.use(express.static('public'));//指定靜態網頁目錄,當瀏覽器發出非HTML檔案請求時,伺服器端就到public目錄尋找這個檔案

如:<link href="/bootstrap/css/bootstrap.css" rel="stylesheet">, 伺服器端就到public/bootstrap/css/目錄中尋找bootstrap.css檔案

好了,本教程先給大家介紹到這裡,後續還會持續給大家更新,謝謝大家一直以來對雲棲社區網站的支援。!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.