nodejs Web服務(Express)

概述html

Express 是一個簡潔而靈活的 node.js Web應用框架, 提供了一系列強大特性幫助你建立各類 Web 應用,和豐富的 HTTP 工具。node

使用 Express 能夠快速地搭建一個完整功能的網站。git

Express 框架核心特性:github

能夠設置中間件來響應 HTTP 請求。express

定義了路由表用於執行不一樣的 HTTP 請求動做。npm

能夠經過向模板傳遞參數來動態渲染 HTML 頁面。json

安裝:segmentfault

npm install expressapi

npm install express --save數組

關聯中間件安裝:

npm install body-parser --save

npm install cookie-parser --save

npm install multer --save

    以上命令會將 Express 框架安裝在當前目錄的 node_modules 目錄中, node_modules 目錄下會自動建立 express 目錄。

body-parser - node.js 中間件,用於處理 (ContentType)JSON, Raw, Text 和 URL 編碼的數據。

cookie-parser - 這就是一個解析Cookie的工具。經過req.cookies能夠取到傳過來的cookie,並把它們轉成對象。

multer - node.js 中間件,用於處理 enctype="multipart/form-data"(設置表單的MIME編碼)的表單數據。

 

Express中間件body-parser

    express項目中一般使用body-parser進行post參數的解析,最經常使用的是其中的json和urlencoded的parser,可分別對以JSON格式的post參數和urlencoeded的post參數進行解析,都可得到一個JSON化的req.body。

參考:

     express項目中使用body-parser

     body-parser github項目

 

Express API:

// 訪問express靜態資源 link
var express = require('express');
var app = express();
app.use(express.static('public'));
// 工程目錄
/npm_modules
/public/index.html
// 啓動
var server = app.listen(8081, function () {
})
// 訪問 http://serverIp:port/index.html

// 用做接口服務器 link
var express = require('express');
var bodyParser = require('body-parser');

// application/x-www-form-urlencoded 編碼解析
var parseForm = bodyParser.urlencoded({extended: false});
// application/json編碼解析
var parseJson = bodyParser.json();

var httpReq = express();
// http://localhost:8081/index/IndexPage.html
httpReq.use(express.static('public'));

function testExpressApi() {
    /**
     * express get請求
     *  http://localhost:8081/obtain/clientOnlineState?name=aa01&password=010203
     */
    httpReq.get('/obtain/clientOnlineState', function (req, resp) {
        console.log(req.query);
        resp.end(JSON.stringify(req.query));
    });

    /**
     * express post請求
     *  http://localhost:8081/update/remoteClientInfo
     *      {"name":"aa01","password":"010203"}
     */
    httpReq.post('/update/remoteClientInfo', parseJson, function (req, resp) {
        console.log(req.body);
        resp.end(JSON.stringify(req.body));
    });

    /**
     * 服務器監聽8081端口
     * @type {*}
     */
    var server = httpReq.listen(8081, function () {
        // var host = server.address().address;
        var port = server.address().port;
        console.log("http://localhost:%s", port)
    });
}

Express開發問題:

Q1.獲取遠程鏈接IP地址

var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;

參考:

     Express.js: how to get remote client address

     nodejs express獲取不了用戶的外網ip地址解決方法

 

參考:

     Express 4.x API 中文手冊 api

     Node.js Express 框架 runoob基礎教程

     nodejs的express使用介紹

     Nodejs學習筆記(五)--- Express安裝入門與模版引擎ejs

     body-parser-github 官方API

     expressjs-install

 

補充知識

P1.POST常見的幾種提交數據方式

服務端一般是根據請求頭(headers)中的 Content-Type 字段來獲知請求中的消息主體是用何種方式編碼,再對主體進行解析。因此說到 POST 提交數據方案,包含了 Content-Type 和消息主體編碼方式兩部分。

application/x-www-form-urlencoded

最多見的 POST 提交數據的方式了。瀏覽器的原生 form 表單,若是不設置 enctype 屬性,那麼最終就會以 application/x-www-form-urlencoded 方式提交數據。

Content-Type 被指定爲 application/x-www-form-urlencoded;其次,提交的數據按照 key1=val1&key2=val2 的方式進行編碼,key 和 val 都進行了 URL 轉碼。大部分服務端語言都對這種方式有很好的支持。例如 PHP 中,$_POST['title'] 能夠獲取到 title 的值,$_POST['sub'] 能夠獲得 sub 數組。

application/json

application/json 這個 Content-Type 做爲響應頭告訴服務端消息主體是序列化後的 JSON 字符串。JSON 格式支持比鍵值對複雜得多的結構化數據。

multipart/form-data

    這又是一個常見的 POST 數據提交的方式。咱們使用表單上傳文件時,必須讓 form 的 enctyped 等於這個值。

text/xml

它是一種使用 HTTP 做爲傳輸協議,XML 做爲編碼方式的遠程調用規範。

參考:

     application/json 四種常見的 POST 提交數據方式

     淺析application/x-www-form-urlencoded和multipart/form-data的區別

相關文章
相關標籤/搜索