node 循序渐进
1. 执行
node helloworld.js
2. http 服务器
建 server.js 文件 - node server.js 跑起来 - 浏览器访问 http://localhost:8888/ (服务器控制台观看访问次数)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
var http = require("http"); var count=(function(){ var num=0; return function(){ num++; return num; } }()); http.createServer(function (request, response) { response.writeHead(200, { "Content-Type": "text/plain" }); response.write("Hello World"); response.end(); console.log('server'+count()); }).listen(8888);
3. http 服务器 模块化方式实现
建主文件 入口 index.js - 写模块化功能并导出对应接口 server.js - 运行 node index - 访问 http://localhost:8888/
index.js
var start=require('./server');
start.start();
server.js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
function handle(request, response) { response.writeHead(200, { "Content-Type": "text/plain" }); response.write("Hello World"); response.end(); console.log('server' + count()); } var count = (function () { var num = 0; return function () { num++; return num; } }()); var http = require("http"); function start(){ http.createServer(handle).listen(8888); console.log("Server working."); } exports.start = start;
4. http 服务器 + router 模块化 整合
index.js
var server=require('./server');
var route=require('./router');
server.server(route.route);
router.js
function route(pathname) {
console.log("About to route a request for " + pathname);
}
exports.route = route;
server.js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
var count = (function () { var num = 0; return function () { num++; return num; } }()); var http = require("http"); var url = require("url"); function server(route){ function handle(request, response) { var pathname = url.parse(request.url).pathname; route(pathname); response.writeHead(200, { "Content-Type": "text/plain" }); response.write("Hello World"); response.end(); console.log('server' + count()); } http.createServer(handle).listen(8888); console.log("Server working."); } exports.server = server;
5. 低耦合 http 服务器
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.server(router.route, handle);
server.js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
var http = require("http"); var url = require("url"); function server(route, handle){ function requestHandle(request, response) { var pathname = url.parse(request.url).pathname; route(handle, pathname); response.writeHead(200, { "Content-Type": "text/plain" }); response.write("Hello World"); response.end(); } http.createServer(requestHandle).listen(8888); console.log("Server working."); } exports.server = server;
router.js
function route(handle, pathname) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname]();
} else {
console.log("No request handler found for " + pathname);
}
}
exports.route = route;
requestHandlers.js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
function start() { console.log("Request handler 'start' was called."); } function upload() { console.log("Request handler 'upload' was called."); } exports.start = start; exports.upload = upload;
改变为
function start() {
var now = new Date().getTime();
while (new Date().getTime() < now + 10000) {
}
return 'start';
}
多标签页顺序访问 http://localhost:8888/start http://localhost:8888/upload 演示阻塞。
Put the favorites in the favorites and empty the favorites.