• node全局变量  node定时器 系统自带的模块 http服务器


    for(var k of Object.keys(global)){console.log(k)}
    global
    clearInterval
    clearTimeout
    setInterval
    setTimeout
    queueMicrotask
    clearImmediate
    setImmediate
    k
    undefined
    > var timer = setTimeout(function(a,b,c){console.log(a,b,c);},1000,1,'a')
    undefined
    > 1 a undefined
    

    setImmediate

    设定执行完回调,即这个调用可能是比较消耗时间的,暂时不阻止主进程的执行,等主进程执行完之后再执行。

    var timer = setImmediate(function(a, b, c) {
      console.log(a, b, c);
    }, 1000, 1, 'a');
    console.log("hello");

    输出:

    hello
    1000 1 'a'


    系统自带的模块

    通过process.moduleLoadList打印的NativeModule可以查看到相关的模块信息。主要的系统模块包括:

    [ 'assert', 'buffer', 'console', 'dns', 'domain', 'events', 'fs', 'module', 'net', 'os', 'path', 'querystring', 'readline', 'repl', 'stream', 'string_decoder', 'timers', 'tty', 'url', 'util', 'vm' ]

    其中最能代表 node.js 的最初思想的是 net, 'events'这两个模块。

     

    module 及其变量

    在 node.js 里通常一个文件就可以是一个模块 并且每个模块都包含下面几个模块变量:

    1. __dirname 获取当前文件所在目录
    2. __filename 获取当前文件名
    3. exports 到 module.exports 的引用
    4. module 当前模块

    1. require() 模块加载的函数
    1.用于创建 TCP/IPC 服务器 */
    const net = require("net");
    /* 自带模块,不需要下载依赖 */
    /* net.Server 类
    1.用于创建 TCP/IPC 服务器 */
    const server = net.createServer((socket) => {
    });
    /* 2.可能响应的事件: a. connection:外部 socket 接入事件 b. listening: 侦听事件 c. close: 关闭事件 d. error: 错误事件 */
    server.on("connection", (socket) => {
        // 无id
        console.log("socket id = " + socket.id);
        console.log("一个新的socket链接");
        /* 获取远程地址与端口 .remoteAddress .remotePort
    
            取消 TCP 缓存数据(Nagle 算法) .setNoDelay
    
            发送数据 .write */
         }); /* listening 事件 比 listen 的回调优先级高。*/ server.on("listening", () => { console.log("监听服务"); console.log(arguments); }); /* error 事件 */ server.on('error', (err) => { // handle errors here throw err; }); /* close 事件 会在所有连接关闭后发出 */ server.on('close', (err) => { console.log("服务关闭"); }); server.listen(process.env.NODE_PORT || 8888);

      http服务器

    const http = require('http');
    const server = http.createServer((req, res) => {
        res.end("Hello world!
    ");
    });
    server.listen(4333);  
    

      

    访问测试

    $> curl http://127.0.0.1:4333
    Hello world!

    最简单的 HTTP 服务器完成!

    下面命令可以看到协议的一些交互信息:

    curl -v http://127.0.0.1:4333
    C:Usersdell>curl http://127.0.0.1:4333
    Hello world!
    
    C:Usersdell>curl -v http://127.0.0.1:4333
    * Rebuilt URL to: http://127.0.0.1:4333/
    *   Trying 127.0.0.1...
    * TCP_NODELAY set
    * Connected to 127.0.0.1 (127.0.0.1) port 4333 (#0)
    > GET / HTTP/1.1
    > Host: 127.0.0.1:4333
    > User-Agent: curl/7.55.1
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Date: Mon, 28 Dec 2020 07:40:32 GMT
    < Connection: keep-alive
    < Content-Length: 13
    <
    Hello world!
    * Connection #0 to host 127.0.0.1 left intact
    
    C:Usersdell>
    

      

  • 相关阅读:
    多线程
    事务的概念与四大特性(ACID)
    Error和Exception的区别
    运算符的使用与注意事项(二)
    运算符的使用与注意事项(一)
    GOJS使用--前端拓扑图
    python算法实现分治法demo
    MySQL主从复制配置
    MySQL锁
    show profile查看SQL执行生命周期
  • 原文地址:https://www.cnblogs.com/fdxjava/p/14201213.html
Copyright © 2020-2023  润新知