zoukankan      html  css  js  c++  java
  • node_原理整理

    node : js运行时环境

        浏览器js    ES+DOM+BOM

        node        ES+核心模块:创建服务器、fs文件操作模块【加分项】

    node可以做什么事情?——流操作,ajax,express,buffer

        1   创建高性能的服务器————单线程

        2   工具

        3   中间件

        4   ??脚手架

        5   -跨越问题:node之间通信

            -命令行工具;浏览器渲染

    =====================================================

    全栈项目:主服务一般不用node;实际商业项目用:javaphp

            -node往往用于搭建接口服务器

    进程>线程:

        进程:表示一个程序。一个进程可以有多个线程。

        线程:JS可以有多个线程【异步任务】——同一时刻:主线程只有一个。

    node:往往适用于 I/O密集,不适合CPU密集(压缩、合并、加迷、解密)

        -不稳定,大型项目不会使用!

    global

    node:

        setImmediate()

        setTimeout()

    node本身的事件环

        eventLoop:跟版本不同 ; node<10以前,>10以后

    //global常见的属性

    Buffer      二进制数据

    process     进程  process.nextTick()    cwd():当前根目录  env:环境 argv:

    clearInterval()     clearTimeout    setInterval setTimeout

    clearImmediate      setImmediate

    ==============================ES6模块化方案===========================

    require js

        node            模块化         

        CommonJS        规范            es6 module

    定义:

        模块:一个文件。模块之间相互独立。{

            -扩展性

            -模块协作

            -

        }

        导入模块: require(模块路径:相对路径);

        导出模块: module.exports;

    node    es+核心模块

    node模块分类:  1核心模块(内置模块)[fshttp...]        2自定义模块        3第三方模块

    node --inspect-brk file.js

    //require('./a')

    1:把相对路径解析成绝对路径

    2:读取该文件

    3:让函数执行

    http模块创建服务器

    const http = require('http');

    //创建服务器

    const app = http.createServer();

    //监听客户端请求

    app.on('request', (req, res) => {

    //响应

    res.end('<h1>hi</h1>');

    console.log(req);

    console.log(req.url);

    console.log(req.method);

    console.log(req.headers);

    });

    app.listen(8888);

    console.log('服务器已启动,请访问localhost:8888');

    获取request请求:http://localhost:8888/?username=macro&pwd=123456

    // http 创建服务器模块

    const url = require('url');

    const http = require('http');

    //创建服务器

    const app = http.createServer();

    //监听客户端请求

    app.on('request', (req, res) => {

    //响应

    // console.log(req.url);

    let url1 = url.parse(req.url);

    let { query } = url.parse(req.url, true);

    res.end(`${query.username}-${query.pwd}`);

    console.log(url.parse(req.url, true));

    //res.end('welcome');

    });

    app.listen(8888);

    console.log('服务器已启动,请访问localhost:8888');

    querystring模块处理request

    const qs = require('querystring');


    app.on('request', (req, res) => {

    //请求

    let postData = ''; //由于浏览器分段传播

    //监听参数传递事件

    req.on('data', (chunk) => {

    postData += chunk;

        });

    // console.log(req.url);

    // console.log(postData);

    req.on('end', () => {

    // username=admin&pwd=123456

    console.log(postData);

    let { username, pwd } = qs.parse(postData);

    console.log(username, pwd);

        });

    // res.end(username, pwd);

    });

    客户端

    服务器端——处理数据和业务逻辑

    客户端 ---请求--->  服务器端

        <-----响应-----   

    服务器集群

    ip地址/域名/

    端口:区分服务

    协议号://域名/资源

    本地ip:127.0.0.1

    本地域名:localhost

    报文:请求和响应的过程中


    响应状态码:

        200 ok

        404 not find

        500 服务器错误

        400 客户端请求有语法错误

        304 缓存错误

    内容类型:

        text/html

        text/css

        text/javascript

        image/jpeg

        application/json

    HTTP请求处理与响应处理

        请求参数

            get请求参数

                get请求:参数会放置在浏览器地址栏中  

                    1:浏览器直接输入网址

                    2:link

                    3: script

                    4: img {src}

                    5: Form表单

            post请求

                    1:参数是被放在请求体或者报文中进行传输

                   2:node处理post请求需要使用dataend事件

                    3:使用querystring模块

    路由:【对应关系】客户端请求地址与服务器端程序代码地对应关系

    //静态网站

    静态资源:服务器不需要处理,可以直接相应给客户端(public)

        mime模块:解决不同网页类型,图片、text、等等

    动态资源:相同地请求地址不同地响应资源

            .../blog/article?id=1

            .../blog/article?id=2

            .../blog/article?id=3


    区别express得路由:

    const http = require('http');

    const url = require('url');

    const app = http.createServer();

    app.on('request', (req, res) => {

    let { pathname } = url.parse(req.url);

    res.writeHead(200, {

    'Content-Type': 'text/html;charset=utf8'

        });

    if (pathname === '/' || pathname === '/index') {

    res.end(`欢迎访问首页`);

        } else if (pathname === '/about') {

    res.end(`欢迎访问关于我们`);

        } else if (pathname === '/news') {

    res.end(`欢迎访问新闻首页`);

        };

    res.end(`404`);

    });

    app.listen(8888);

    npm包分类:

        全局包: 命令行使用

            安装第三方: npm install *** --save -g

            自己创建全局包

        本地包: 项目中使用

            npm install jquery  //默认安装的是生产依赖

            生产依赖放在 “dependencies":{} 以后删除依赖可以通过 npm install 安装所有依赖

            开发依赖:"devDependencies"

            npm install @babel/core --save-dev(-D)

            npm install --production //安装所有生产依赖

            //

            其他依赖: 'peerDependecies':{}

                    'bundledDependencies'

    3 package.json

        npm init 初始化一个文件

        npm init -y 全部使用默认值

        npm uninstall *** -g

    nodemon:

        1:每次源文件修改后,自动执行

        2:安装 npm i nodemon -g

        3: nodemon index

    npm :  

    nrm :npm registry manager  npm下载地址切换工具

    nvm :node version manager

        npm i nrm -g

        nrm ls

        nrm use cnpm  //切换为淘宝源

        nrm current

    package-lock.json   : 锁定包的版本

    函数问题

    function Fibonacci(num) {

    if (num === 0) {

    return 0;

                } else if (num === 1) {

    return 1;

                } else {

    return Fibonacci(num - 2) + Fibonacci(num - 1);

                }

            }

    var num = parseInt(prompt("input the numb"));

    var res = '';

    for (var index = 0; index <= num; index++) {

    res += Fibonacci(index) + ",";

            }

    console.log(res);

  • 相关阅读:
    大气漂亮美观的三个网站后台数据管理模板 大大提升你的开发效率
    9个设计师常用的高清图库 不敢配图? 这9个免版权图库牢记心中!
    《PHP制作个人博客》之四:分类添加及前端导航数据用php动态调取
    《全栈营销之如何制作个人博客》之二:php环境安装及个人博客后台搭建 让你的博客跑起来
    《全栈营销之如何制作个人博客》之一:用什么开发语言和CMS系统
    adobe 2020版全家桶免费破解版 最新pr, photoshop ae 免费使用
    三个装修 家具 装饰公司网站模板 大气美观 简单明了的静态模板
    程序员摆地摊能接到活吗?码农地摊卖什么能挣到外快钱?
    phpstorm2020最新版激活方法 永久更新
    五个最适合做博客的开源系统 开源免费大量精美模板使用!
  • 原文地址:https://www.cnblogs.com/macro-renzhansheng/p/13044756.html
Copyright © 2011-2022 走看看