Node入门


刚看了《Node入门》,下面是里面所教的一个简单的web应用。使用的是事件驱动的异步编程。

一共四个文件,index.js, server.js, router.js, requestHandler.js。看完代码再来分析

//index.js
var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");

var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;

server.start(router.route, handle);


//server.js
var http = require("http");
var url = require("url");

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

    route(handle, pathname, response);
  }

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

exports.start = start;

//router.js
function route(handle, pathname, response) {
  console.log("About to route a request for " + pathname);
  if (typeof handle[pathname] === 'function') {
    handle[pathname](response);
  } else {
    console.log("No request handler found for " + pathname);
    response.writeHead(404, {"Content-Type": "text/plain"});
    response.write("404 Not found");
    response.end();
  }
}

exports.route = route;
 
//requestHandler.js
var exec = require("child_process").exec;

function start(response) {
  console.log("Request handler 'start' was called.");

  exec("ls -lah", function (error, stdout, stderr) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write(stdout);
    response.end();
  });
}

function upload(response) {
  console.log("Request handler 'upload' was called.");
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello Upload");
  response.end();
}

exports.start = start;
exports.upload = upload;

Node能够很好地支持高并发,就是因为它能够通过「函数」传递来非阻塞地响应请求。

在Python里,通常使用了return来返回需要的内容给前端,但是在Node里,我们可以直接往response里写入需要返回的内容。所以,上面的代码通过一层一层把response对象传递来实现非阻塞。

function onRequest(request, response)->route(handle, pathname, response)->handle[pathname](response)->response.write("Hello Upload"), 一层一层地传递。所以,每次有请求的时候,就交给onRequest处理,不用等onRequest处理完成,就可以接受下一个请求,所以说是事件驱动的,也因此是非阻塞的。

YOU MIGTH ENJOY

  • System design 4 - How to design a rate limiter
  • How I brought my cat to Canada 如何把猫咪带去加拿大
  • System Design 3 - 知乎匿名系统设计