iT邦幫忙

DAY 4
0

Node.js 學習筆記系列 第 4

Node.js學習筆記三:Blocking與Non-blocking的思考

在上一篇的Node.js路由上,我們的例子只輸出到console,而沒有顯示在網頁上。要顯示到網頁上其實很簡單,只要根據不同的頁面返回不同的內容,再透過Response(回應)輸出到網頁。

一、根據路由的網頁輸出

三個文件都需要做簡單的改動,先看showPage.js:

function home() {
  console.log("This is the home page.");
  return "This is home page.";
}

function blog() {
  console.log("This is the blog page.");
  return "This is blog page.";
}

exports.home = home;
exports.blog = blog;

再看route.js:

var showPage = require("./showPage");

function route(pathname) {

  var handle = {}
  handle["/"] = showPage.home;
  handle["/blog"] = showPage.blog;

  if (typeof handle[pathname] === 'function') {
    return handle[pathname]();
  } else {
    console.log("404 Not Found " + pathname);
    return "404 Not Found.";
  }
}

exports.route = route;

最後是index.js:

var http = require("http");
var url = require("url");
var router = require("./route");

function onRequest(req, res) {
  var pathname = url.parse(req.url).pathname;

  var content = router.route(pathname);

  res.writeHead(200, {"Content-Type": "text/plain"});
  res.end(content);
}

http.createServer(onRequest).listen(3000);
console.log("Server has started to listen at port: 3000.");

重新啟動你的程式,會看到:localhost:3000/ 與 localhost:3000/blog 顯示我們要求的內容。

二、把非同步也考慮進去。

這樣的做法的問題是,如果你在home()裡進行大量的處理,需要較長的載入時間的話,當你在等 localhost:3000/ 的時候連 localhost:3000/blog 也進不去,等home()處理完後才會執行blog(),也就是home()把之後的程式都block住了。這不是違反Node.js的**非同步(Asynchronous)**特性嗎?原因是Node.js是單執行緒(Single Thread),但I/O是非同步,具體是如何呢?想要瞭解更多的,請看這篇文章。 在這裡,我們知道I/O是不會被block掉,而我們又擔心函數執行太久影響輸出,最簡單的解決方法就是直接在函數裡處理輸出。要實現這個想法,只要把Response一直傳到home()跟blog()。 這是修改後的index.js:

var http = require("http");
var url = require("url");
var router = require("./route");

function onRequest(req, res) {
  var pathname = url.parse(req.url).pathname;

  router.route(pathname,res);

}

http.createServer(onRequest).listen(3000);
console.log("Server has started to listen at port: 3000.");

然後是route.js:

var showPage = require("./showPage");

function route(pathname, res) {

  var handle = {}
  handle["/"] = showPage.home;
  handle["/blog"] = showPage.blog;

  if (typeof handle[pathname] === 'function') {
    return handle[pathname](res);
  } else {
    console.log("404 Not Found " + pathname);
    res.writeHead(200, {"Content-Type": "text/plain"});
    res.end("404 Not Found " + pathname);
  }
}

exports.route = route;

最後是showPage.js:

function home(res) {
  console.log("This is the home page.");
  res.writeHead(200, {"Content-Type": "text/plain"});
  res.end("This is home page.");
}

function blog(res) {
  console.log("This is the blog page.");
  res.writeHead(200, {"Content-Type": "text/plain"});
  res.end("This is blog page.");
}

exports.home = home;
exports.blog = blog;

在這個例子中,是看不到任何的變化,我只想帶出I/O是非同步的概念。

三、總結

今天的重點只有一個:

1. Node.js是single thread, I/O是非同步(Asynchronous)。

到這裡為止,大家對Node.js應該有相當的概念,下一篇我們進入Express.js吧。Express.js是專為Node.js設計的網頁框架,有了它,用Node.js寫網頁就簡單多了。

(本文同步發表於NodeJust.com


上一篇
Node.js學習筆記二:Node.js路由(Routing)處理基礎
下一篇
Node.js學習筆記四:Express.js的安裝使用
系列文
Node.js 學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言