zoukankan      html  css  js  c++  java
  • node 循序渐进

    1. 执行   

    node helloworld.js

    2. http  服务器 

    建 server.js 文件 -  node server.js  跑起来 -  浏览器访问  http://localhost:8888/  (服务器控制台观看访问次数)

    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);
    View Code

    3. http  服务器   模块化方式实现

    建主文件 入口  index.js   -   写模块化功能并导出对应接口 server.js  -   运行 node index  -  访问 http://localhost:8888/ 

    index.js

    var start=require('./server');
    start.start();
    server.js
    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;
    View Code

     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
    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;
    View Code

     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
    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;
    View Code

    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
    function start() {
      console.log("Request handler 'start' was called.");
    }
    
    function upload() {
      console.log("Request handler 'upload' was called.");
    }
    exports.start = start;
    exports.upload = upload;
    View Code

     改变为

    function start() {
    var now = new Date().getTime();
    while (new Date().getTime() < now + 10000) {

    }
    return 'start';
    }
    多标签页顺序访问    http://localhost:8888/start    http://localhost:8888/upload    演示阻塞。
     
     
     
     
     
     
     
     


  • 相关阅读:
    MySQL 初识别语句,数据库、表、行的增删改查
    mysql如何从全备文件中恢复单个库或者单个表
    Shell 同步时间脚本
    app手机端连接tomcat电脑端服务器
    大于号转义符&gt;---小于号转义符&lt;
    轻松实现页面提交中
    重复提交问题(一)
    json
    ExtJs 6.0+快速入门,ext-bootstrap.js文件的分析,各版本API下载(一)
    ExtJS 6 如何引入Dashboard模版
  • 原文地址:https://www.cnblogs.com/justSmile2/p/9896848.html
Copyright © 2011-2022 走看看