Node.js 路由,前端异步流程工具

 

自定义包上传npm.js

注意: 记住要发送邮箱激活 注意: 切换你的npm源 nrm 注意: 你的包的名称不要和别人的重复 步骤: 1.登录npm账号 npm adduser

  1. 上传

    npm publish

node后端路由基本原理

  • 通过htpp模块来做。其实后端路由就是web服务器

  • 路由的产生:

    *在之前,是多页面的,点一个链接开一个新网页。

    *现在流行的是单页面,只有一个index,使用模板替换里面的内容。

const http = require('http')
​
const host = 'localhost'
​
const fs = require('fs')
​
const port = 6868;
​
http.createServer((req, res) => {
        res.writeHead(200, {
            'Content-type': 'text/html;charset=utf8'
        })
        switch (req.url) {
            case '/home':
                res.write('home')
                res.end()
​
                break;
            case '/shopcar':
                fs.readFile('./static/shopcar.html', 'utf8', (error, docs) => {
                    res.write(docs)
                    res.end()
                })
                break;
            case '/1.jpg':
                fs.readFile('./static/1.jpg', (error, docs) => {
​
                    res.writeHead(200, { 'Content-Type': 'image/jpeg' }); //输出类型
                    // 图片是以二进制传输的
​
                    res.write(docs, 'binary')
                    res.end()
                })
                break;
            case '/index.js':
                fs.readFile('./static/js/index.js', (error, docs) => {
                    // 图片是以二进制传输的
                    res.write(docs)
                    res.end()
                })
                break;
            default:
                break;
        }
​
    })
    .listen(port, host, () => {
        console.log(`服务器运行在:http://${ host }:${ port }`)
    })

 

npm脚本

概念: npm脚本指的是package.json中的scripts字段

认识package.json

  • package.json 是记录项目依赖包信息和npm脚本命令的一个配置文件

    项目依赖包信息:

    扫描二维码关注公众号,回复: 6815011 查看本文章

    *dependencies 生产环境的依赖包

    *devDependencies 开发环境使用的依赖包

  • 脚本命令:

    *npm init -y 初始化

    *npm run dev & npm run app 全部执行完命令,最后输出结果

    *npm run dev && npm run app 依次执行

 1 //分别创建case.js与case2.两个文件
 2  3 {
 4     "name": "3-jiaoben",
 5     "version": "1.0.0",
 6     "description": "",
 7     "main": "case.js",
 8     "scripts": {
 9         "test": "echo \"Error: no test specified\" && exit 1",
10         "dev": "node case.js",
11         "app": "node case2.js"
12 13     },
14     "keywords": [],
15     "author": "",
16     "license": "ISC"
17 }

 

前端异步流程工具

javascript是单线程,依次执行一个任务,想要让任务能够顺利执行,就需要使用异步。将任务放入异步队列中,在主线程执行结束之后再去执行队列的任务

  • 前端异步的操作方式

    1:传统方式:回调函数,事件

    2:前端异步流程工具(封装出来的函数,库):

    es6 Promise

    es6 Generator

    es6-es8 async 函数

    Node中的异步处理工具--> nextTi , steImmediate

    第三方类库-->async.js

  • Pormise

     1 const pse1 = new Promise((resolve, reject) => { //resolve 将未完成变为成功,reject将未完成变为失败
     2         resolve('任务一')
     3     }).then(data => {
     4         console.log(data);
     5  6     })
     7     .catch(error => {
     8         if (error) throw error; //抛出一个错误,这个时候进程会被中断
     9     })
    10 11 12 const pse2 = new Promise((resolve, reject) => {
    13         setTimeout(() => {
    14             resolve('任务二')
    15         }, 2000)
    16     }).then(data => {
    17         console.log(data);
    18 19     })
    20     .catch(error => {
    21         if (error) throw error;
    22     })
    23 24 //promise 提供的两个核心方法
    25 //Promise.all([promise实例1, promise实例2])
    26 //Promise.race([promise实例1, promise实例2])
    27 28 29 // Promise
    30 //     .all([pse1, pse2]) //执行所有任务
    31 //     .then(() => { //(data)=>{}  没有值可以不给
    32 //         console.log('任务三');
    33 //     })
    34 //console.log('主线程');
    35 //此时输出:主线程,任务一,任务二,任务三
    36 37 Promise
    38     .race([pse1, pse2])
    39     .then(() => {
    40         console.log('任务三');
    41 42     })
    43 console.log('主线程');
    44 //此时输出:主线程,任务一,任务三,任务二
    45 46 //all 依次执行任务,即使有延时任务,也必须等延时任务介绍才能执行后续任务
    47 //race 谁快谁先执行![1563348372761](C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1563348372761.png)

参考资料

  1. Promise https://blog.csdn.net/MrJavaweb/article/details/79475949

  2. Generator https://www.cnblogs.com/imwtr/p/5913294.html

  3. Async-await

    • 里层请求数据结果返回到外层使用

    • Async函数式generator函数 + spawn 自动执行器函数的 封装

  4. Node.js 中的nextTick()和setimmediate() https://www.cnblogs.com/5ishare/p/5268273.html

  5. async库 https://caolan.github.io/async/

 

猜你喜欢

转载自www.cnblogs.com/jxqw/p/11204859.html
今日推荐