Node.js系列八 - koa开发web服务器

2021-02-07  本文已影响0人  懂会悟

1、 Koa初体验

1.1. 认识Koa

Koa官方的介绍:

事实上,koa和express是同一个团队开发的一个新的Web框架:


1.2. koa初体验

koa与express的基本开发模式是比较相似的

// koa 的Web服务器
const Koa = require('koa');

const app = new Koa();

app.use((ctx, next) => {
  console.log("middleware 01");
  next();
})

app.use((ctx, next) => {
  console.log("middleware 02");
  ctx.response.body = "Hello World";
})


app.listen(8000, () => {
  console.log("服务器启动成功~");
});

koa注册的中间件提供了两个参数:

koa通过创建的app对象,注册中间件只能通过use方法:

但是真实开发中我们如何将路径和method分离呢?

// 方式一:根据request自己判断
app.use((ctx, next) => {
  if (ctx.request.path === '/users') {
    if (ctx.request.method === 'POST') {
      ctx.response.body = "Create User Success~";
    } else {
      ctx.response.body = "Users List~";
    }
  } else {
    ctx.response.body = "Other Request Response";
  }
})

整个代码的逻辑是非常复杂和混乱的,真实开发中我们会使用路由。


1.3. 路由的使用

koa官方并没有给我们提供路由的库,我们可以选择第三方库:koa-router

安装koa-router

npm install koa-router

koa-router基本使用

// user.router.js
const Router = require('koa-router');

const userRouter = new Router();

userRouter.get('/users', (ctx, next) => {
  ctx.response.body = "user list~";
});

userRouter.post('/users', (ctx, next) => {
  ctx.response.body = "create user info~";
});

module.exports = userRouter;

在app中将router.routes()注册为中间件:

app.use(userRouter.routes());
app.use(userRouter.allowedMethods());

注意 : allowedMethods用于判断某一个method是否支持:

router的前缀

通常一个路由对象是对一组相似路径的封装,那么路径的前缀都是一直的,所以我们可以直接在创建Router时,添加前缀:

const userRouter = new Router({ prefix: '/users' });

userRouter.get('/', (ctx, next) => {
  ctx.response.body = "user list~";
});

userRouter.post('/', (ctx, next) => {
  ctx.response.body = "create user info~";
});

module.exports = userRouter;

1.4. 请求解析

常见的客户端传递到服务器参数的5种方法:

方式一:params

请求地址:http://localhost:8000/users/123

获取params:

const userRouter = new Router({prefix: "/users"})

userRouter.get("/:id", (ctx, next) => {
  console.log(ctx.params.id);
  ctx.body = "Hello World";
})

方式二:query

请求地址:http://localhost:8000/login?username=why&password=123

获取query:

app.use((ctx, next) => {
  console.log(ctx.request.query);
  ctx.body = "Hello World";
})

方式三:json

请求地址:http://localhost:8000/login

body是json格式:

{
    "username": "Tom",
    "password": "123456"
}

获取json数据:

app.use(bodyParser());

方式四:x-www-form-urlencoded

请求地址:http://localhost:8000/login

body是x-www-form-urlencoded格式:

获取json数据:(和json是一致的)

app.use(bodyParser());

方式五:form-data

请求地址:http://localhost:8000/login

body是form-data格式:

解析body中的数据,我们需要使用multer

const upload = multer({});

app.use(upload.any());

app.use((ctx, next) => {
  console.log(ctx.req.body);
  ctx.body = "Hello World";
});

multer还可以实现文件的上传:

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, "./uploads/")
  },
  filename: (req, file, cb) => {
    cb(null, Date.now() + path.extname(file.originalname))
  }
})

const upload = multer({
  storage
});

const fileRouter = new Router();

fileRouter.post("/upload", upload.single('avatar'), (ctx, next) => {
  console.log(ctx.req.file);
})

app.use(fileRouter.routes());


1.5. 响应方式

输出结果:body

将响应主体设置为以下之一:

如果response.status尚未设置,Koa会自动将状态设置为200或204。

比较常见的输出方式:

ctx.response.body = "Hello World";
ctx.body = {
  name: "Tom",
  age: 18,
  height: 1.88
};
ctx.body = ["abc", "cba", "nba"];

疑惑:ctx.response.body和ctx.body之间的区别:

请求状态:status

请求状态我们可以直接给ctx设置,或者给ctx.response设置也是一样的效果:

ctx.status = 201;
ctx.response.status = 204;

1.6. 错误处理

const Koa = require('koa');

const app = new Koa();

app.use((ctx, next) => {
  ctx.app.emit('error', new Error("出错啦"), ctx);
})

app.on('error', (err, ctx) => {
  console.log(err.message);
  ctx.response.body = "出错啦";
})

app.listen(8000, () => {
  console.log("错误处理服务启动成功~");
})

1.7. 静态服务器

koa并没有内置部署相关的功能,所以我们需要使用第三方库:

npm install koa-static

部署的过程类似于express:

const Koa = require('koa');
const static = require('koa-static');

const app = new Koa();

app.use(static('./build'));

app.listen(8000, () => {
  console.log("静态服务器启动成功~");
});

2、koa 与 express的比较

在学习了两个框架之后,我们应该已经可以发现koa和express的区别:

从架构设计上来说:

因为express和koa框架他们的核心其实都是中间件:

const express = require('express');

const app = express();

const middleware1 = (req, res, next) => {
  req.message = "middleware1";
  next();
  res.end(req.message);
}

const middleware2 = (req, res, next) => {
  req.message = req.message + 'middleware2';
  next();
}

const middleware3 = (req, res, next) => {
  req.message = req.message + 'middleware3';
}

app.use(middleware1, middleware2, middleware3);

app.listen(8000, () => {
  console.log("启动成功~");
})

// middleware1middleware2middleware3
const Koa = require('koa');

const app = new Koa();

const middleware1 = (ctx, next) => {
  ctx.message = "middleware1";
  next();
  ctx.body = ctx.message;
}

const middleware2 = (ctx, next) => {
  ctx.message = ctx.message + 'middleware2';
  next();
}

const middleware3 = (ctx, next) => {
  ctx.message = ctx.message + 'middleware3';
}

app.use(middleware1);
app.use(middleware2);
app.use(middleware3);

app.listen(8001, () => {
  console.log("启动成功~");
})

// middleware1middleware2middleware3
const express = require('express');
const axios = require('axios');
const app = express();

const middleware1 = async (req, res, next) => {
  req.message = "middleware1";
  await next();
  res.end(req.message);
}

const middleware2 = async (req, res, next) => {
  req.message = req.message + 'middleware2';
  await next();
}

const middleware3 = async (req, res, next) => {
  const result = await axios.get('http://123.207.32.32:9001/lyric?id=167876');
  req.message = req.message + result.data.lrc.lyric;
  console.log(req.message);
}

app.use(middleware1, middleware2, middleware3);

app.listen(8000, () => {
  console.log("启动成功~");
})

// middleware1middleware2 
// express 中 next()本身是一个同步函数 不等待异步请求的返回
// express 底层是不支持Async/Await 的
const Koa = require('koa');
const axios = require('axios');
const app = new Koa();

const middleware1 = async (ctx, next) => {
  ctx.message = "middleware1";
  await next();
  ctx.body = ctx.message;
}

const middleware2 = async (ctx, next) => {
  ctx.message = ctx.message + 'middleware2';
  await next();
}

const middleware3 = async (ctx, next) => {
  const result = await axios.get('http://123.207.32.32:9001/lyric?id=167876');
  console.log(result)
  ctx.message = ctx.message + result.data.lrc.lyric;
}

app.use(middleware1);
app.use(middleware2);
app.use(middleware3);

app.listen(8001, () => {
  console.log("启动成功~");
})

// Koa2 底层已经原生支持Async/Await 

上一篇 下一篇

猜你喜欢

热点阅读