• Node.js(一)


      Node.js

                              基于V8 引擎  非阻塞io 单线程事件驱动

    1. node.js的特点介绍  

      单线程

        优点 : 所需要的资源少

        缺点: 单线程 如果某个用户访问的时候 服务器炸了, 别人就不能访问了

      因为是单线程,又要高性能,就不能是阻塞io,因为单线程,还是非阻塞io所以会很乱,所以就需要事件驱动

      node.js 是使用 javaScript 进行编程   运行在v8引擎上

      php 、jsp 等不同,node.js   不需要简历在服务器软件智商

    2.去官网下载 node  和 git

    http://nodejs.cn/download/

    https://git-scm.com/downloads

    安装好node.js  验证是否安装成功 ,可以在gitbash用  node -v 查看node版本

    3.windows下使用gitBash 用的是 linux命令 长用命令如下 

    Windows命令  Linux命令  意义
    cd e:xxx cd/e/xx  切换到e盘下xxx目录
    cd  pwd  显示当前目录路径
    dir    ls  列出当前目录的内容
    copy nul xx.txt touch xxx.txt  生成名为xxx.txt的空文件
    del xxx.txt rm xxx.txt  删除xxx.txt
    md xxx mkdir xxx  创建xxx目录
    rd /s xxx rm-r xxx  删除xxx目录

    node.js   的控制台很乱的时候可以用    cls     清屏

     

    3.模块之间访问

      module1.js

    function say (){
      console.log('折历史 module1')
    }
    module.exports = {  //导出say函数
      say:say,
    }

      module2.js

    var module1 = require('./module1.js');  //请求module1.js
    module1.say();

      require('/module1/')  是请求当前文件夹  里的   index .js

      require('/m1/')   是先查看时候有  m1.js的文件,如果有就请求 m1.js 如果没有 就看看有没有m1文件夹,看m1文件夹里面有没有  index.html 如果有就请求  没有就报错

      导出有两种方式  

      1. exports.add = add;  这么写  在require时  得到的add 是挂在在接受容器上的 

      2.modle.exports = add;  这么写   则 可以直接用接收容器执行

    3.使用node.js搭建web server

      1.请求 http 模块

    var http = require('http');
    
    let server = http.createServer(// 发送请求
      (req,res)=>{
        res.writeHead(200,'content-type','text/html;charset=utf-9');    //请求头
        res.write('write');//访问一次执行一次
        res.end('end');//回调函数  在请求时才执行
      }
    )
    console.log('这里不必等服务器完全开启 就会打印出来 因为node.js是异步的') server.listen(
    4000);

      gitbash 切换到当前目录    node 文件名   来运行文件

      在浏览器   打开  127.0.0.1:4000/localhost:4000  查看发送的数据

    4.npm (node package module)

       设置包管理 显示进度

    npm config set log-level http
    
    npm config set loglevel=http

      设置淘宝镜像

    npm install cnpm -g --registry=https://registry.npm.taobao.org

      npm 安装underscore.js 并使用

    //gitbash
    npm i -g underscore
    //js
    let _ = require('underscore'); let arr = [1,2,3,4,5,6]; arr = _.without(arr,5); console.log(arr) //[1,2,3,4,6]

    5.pm2  如果服务器宕机, 自动重启 (类似的进程管理工具 还有  supervisor forever)

      1).下载 pm2

    npm install -globall pm2

      2).用pm2  执行 demo.js

    pm2 start demo.js

      3).停止demo.js的 pm2

    pm2 stop demo.js  //如果启用后不停止,端口会一直被占用

      4).关闭所有pm2

    pm2 kill

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    单调栈的四种用法
    AtCoder Beginner Contest 223 F Parenthesis Checking(线段树)
    牛客寒假算法基础集训营4 B 进制(线段树)
    AtCoder Beginner Contest 243 E Edge Deletion(最短路)
    AtCoder Beginner Contest 238 E E Range Sums(建图)
    CodeForces 1632D New Year Concert(ST 表、二分)
    【windows terminal】cmd
    【vue3】监控响应: reactive/watch/watchEffect/provide.inject/computed
    Spring DM Application Context
    Android学习历程(一)结构理解
  • 原文地址:https://www.cnblogs.com/96weibin/p/8574122.html
Copyright © 2020-2023  润新知