zoukankan      html  css  js  c++  java
  • express ,koa1, koa2学习笔记

    express

    express就是传统的回调函数

    app.use(),公用的,所有的路由都会用到,

    app.use(express.static('public'));

    app.set(),设置模版引擎,使用swig

    var swig = require('swig');
    app.set('view engine','html');
    app.engine('html', swig.renderFile);

    app.get(),设置路由

    app.get('/', function(req, res, next){
      res.render('index',{
        title: '测试首页',
        content: 'hello world'
      });
    })

    express连接数据库,使用mysql插件

    var mysql = require('mysql');
    
    var connection = mysql.createConnection({
      host     : 'localhost',
      user     : 'root',
      password : '123456',
      database : 'school'
    });
     
    connection.connect();
    var post = {
            username: req.query.username
        }
    
        var query = connection.query('INSERT INTO userinfo SET ?', post, function (error, results, fields) {
              if (error) throw error;
              // Neat!
              res.json({
                success: 'ok'
            });
        });

    koa1

    koa1使用的是generate函数,yield

    不能使用get定义路由了,只能使用外部的插件,并且use方法的回调函数是generate函数

    app.use(function *(next){

      console.log(1);

      yield next;

      console.log(4);

    })

    app.use(function *(next){

      console.log(2);

      yield next;

      console.log(3);

    })

    输出是 1,2,3,4

    所有的路由都会经过这些

    还有引入koa-router,没有先后顺序的,是因为我写的generate函数没有yield next。。。

    var koa = require('koa');
    var router = require('koa-router')();
    var app = koa();
    app.use(router.routes());
    app.use(router.allowedMethods());
    
    router.get('/', function *(next){
        console.log('顺序4');
        this.body = 'Hello world';
    });

    request-promise

    promise支持的简单的http请求插件。

    https://www.npmjs.com/package/request-promise

    koa2

    koa2使用的是async/await

    需要使用new初始化koa

    const Koa = require('koa');
    const app = new Koa();

    app.use里面可以直接是函数,箭头函数,async/await函数,不支持直接写generate函数,需要包裹co模块

    const co = require('co');
    app.use(co.wrap(function *(ctx,next){
        console.log(2);
        const start = new Date();
        yield next();
        console.log(5);
        const ms = new Date() - start;
        console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
    
        //ctx.body = 'hello world';
    }))

    箭头函数的写法

    app.use((ctx,next) => {
        console.log(1);
        const start = new Date();
        return next().then(()=>{
            console.log(6);
            const ms = new Date() - start;
            console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
        });
        //ctx.body = 'hello world';
    })

    相当于又包裹了一层回调函数,next是一个方法中间件,返回一个promise对象,可以使用then方法执行后续的内容

    // Middleware normally takes two parameters (ctx, next), ctx is the context for one request,
    // next is a function that is invoked to execute the downstream middleware. It returns a Promise with a then function for running code after completion.
     

    async/await

    app.use(async(ctx,next) => {
        console.log(3);
        const start = new Date();
        await next();
        console.log(4);
        const ms = new Date() - start;
        console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
        //ctx.body = 'hello world';
    })

    执行顺序和koa1相同,都是先顺序后倒序。

    koa1,2都需要借助外部的很多插件来完成项目的搭建。

  • 相关阅读:
    hive笔记
    hive数据倾斜的解决办法
    Kafka笔记7
    kafka笔记6
    Kafka笔记5
    kafka笔记4(2)
    kafka笔记4
    Kafka笔记3
    kafka笔记2
    kafka笔记1
  • 原文地址:https://www.cnblogs.com/wenwenli/p/8806545.html
Copyright © 2011-2022 走看看