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都需要借助外部的很多插件来完成项目的搭建。

 

转载于:https://www.cnblogs.com/wenwenli/p/8806545.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值