帶著這些知識(shí),我們?cè)賮?lái)看看我們簡(jiǎn)約而不簡(jiǎn)單的HTTP服務(wù)器:
var http = require("http");
http.createServer(function(request, response) {
? response.writeHead(200, {"Content-Type": "text/plain"});
? response.write("Hello World");
? response.end();
}).listen(8888);
現(xiàn)在它看上去應(yīng)該清晰了很多:我們向?createServer?函數(shù)傳遞了一個(gè)匿名函數(shù)。
用這樣的代碼也可以達(dá)到同樣的目的:
var http = require("http");
function onRequest(request, response) {
? response.writeHead(200, {"Content-Type": "text/plain"});
? response.write("Hello World");
? response.end();
}
http.createServer(onRequest).listen(8888);
也許現(xiàn)在我們?cè)搯?wèn)這個(gè)問(wèn)題了:我們?yōu)槭裁匆眠@種方式呢?
更多建議: