Like Share Discussion Bookmark Smile

J.J. Huang   2020-01-27   Node.js   瀏覽次數:次   DMCA.com Protection Status

Node.js | Router(路由)

簡介

我們要為路由提供請求的URL和其他需要的GETPOST參數,隨後路由需要根據這些資料來執行相應的程式。

因此,我們需要查看HTTP請求,從中提取出請求的URL以及GET/POST參數。這一功能應當屬於路由還是伺服器(甚至作為一個模組自身的功能)確實值得探討,但這裡暫定其為我們的HTTP伺服器的功能。

我們需要的所有資料都會包含在request對像中,該對像作為onRequest()回調函數的第一個參數傳遞。但是為了解析這些資料,我們需要額外的Node.JS模組,它們分別是urlquerystring模組。

1
2
3
4
5
6
7
8
9
10
11
12
13
                   url.parse(string).query
|
url.parse(string).pathname |
| |
| |
------ -------------------
http://localhost:8888/start?foo=bar&hello=world
--- -----
| |
| |
querystring.parse(queryString)["foo"] |
|
querystring.parse(queryString)["hello"]

範例

index.js

1
2
3
4
var server = require("./server");
var router = require("./router");

server.start(router.route);

server.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var http = require("http");
var url = require("url");

function start(route) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");

route(pathname);

response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}

http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}

exports.start = start;

router.js

1
2
3
4
5
function route(pathname) {
console.log("About to route a request for " + pathname);
}

exports.route = route;

執行

1
2
$ node index.js
Server has started.

瀏覽localhost:888

瀏覽結果如下:

1
2
3
4
5
6
7
8
Request for / received.
About to route a request for /

// 以下是robots.txt和favicon.ico的Request
Request for /robots.txt received.
About to route a request for /robots.txt
Request for /favicon.ico received.
About to route a request for /favicon.ico

補充

修改server.jspathnamequeryString取出並印出。

server.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
var http = require("http");
var url = require("url");
var querystring = require("querystring");

function start(route) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");

route(pathname);

var queryString = url.parse(request.url).query;
console.log("Request queryString: " + queryString);
console.log("Request queryString abcd value: " + querystring.parse(queryString)["abcd"]);
console.log("Request queryString efgh value: " + querystring.parse(queryString)["efgh"]);

response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}

http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}

exports.start = start;

瀏覽http://localhost:8888/test?abcd=1234&efgh=5678

瀏覽結果如下:

1
2
3
4
5
6
7
8
9
10
Request for /test received.
About to route a request for /test
Request queryString: abcd=1234&efgh=5678
Request queryString abcd value: 1234
Request queryString efgh value: 5678
Request for /favicon.ico received.
About to route a request for /favicon.ico
Request queryString: null
Request queryString abcd value: undefined
Request queryString edgh value: undefined

註:以上參考了
Node.js 路由