1.http response的类型
const Koa = require('koa');
const app = new Koa();
const main = ctx => {
if (ctx.request.accepts('xml')) {
ctx.response.type = 'xml';
ctx.response.body = '<data>hello world!</data>'
} else if (ctx.request.accepts('json')) {
ctx.response.type = 'json';
ctx.response.body = {data: 'hello world'};
} else if (ctx.request.accepts('html')) {
ctx.response.type = 'html';
ctx.response.body = '<p>hello world</p>';
} else {
ctx.response.type = 'text';
ctx.response.body = 'hello world';
}
};
app.use(main);
app.listen(3000);
2.网页模板
const Koa = require('koa');
const fs = require('fs');
const app = new Koa();
const main = ctx => {
ctx.response.type = 'html';
ctx.response.body = fs.createReadStream('./index.html');
};
app.use(main);
app.listen(3000);
3.路由:koa-route模块
const Koa = require('koa');
const route = require('koa-route');
const app = new Koa();
const main = ctx => {
ctx.response.body = 'hello world';
};
const about = ctx => {
ctx.response.type = 'html';
ctx.response.body = '<a href="/">首页</a>'
};
app.use(route.get('/',main));
app.use(route.get('/about',about));
app.listen(3000);
4.静态资源:koa-static模块
const Koa = require('koa');
const static = require('koa-static');
const path = require('path');
const app = new Koa();
const main = static(path.join(__dirname));
app.use(main);
app.listen(3000);
5.重定向
const Koa = require('koa');
const route = require('koa-route');
const app = new Koa();
const redirect = ctx => {
ctx.response.redirect('/');
ctx.response.body = '<a href="/">首页</a>'
};
const main = ctx => {
ctx.response.body = 'hello,node';
};
app.use(route.get('/',main));
app.use(route.get('/redirect',redirect));
app.listen(3000);
6.中间件栈:koa洋葱圈模型的理解
const Koa = require('koa');
const app = new Koa();
app.listen(3000);
const one = (ctx,next) => {
ctx.response.body = '>> one\n';
next();
ctx.response.body += '<< one\n';
};
const two = (ctx,next) => {
ctx.response.body += '>> two\n';
next();
ctx.response.body += '<< two\n';
};
const three = (ctx,next) => {
ctx.response.body += '>> three\n';
next();
ctx.response.body += '<< three\n';
};
app.use(one);
app.use(two);
app.use(three);
7.异步中间件
const Koa = require('koa');
const fs = require('fs');
const app = new Koa();
const main = async (ctx,next) => {
ctx.response.type = 'html';
ctx.response.body = await fs.createReadStream('./index.html','utf-8');
};
app.use(main);
app.listen(3000);
8.中间件的合成:koa-compose模块
const Koa = require('koa');
const compose = require('koa-compose');
const app = new Koa();
const logger = (ctx,next) => {
console.log(`${Date.now()} ${ctx.request.method} ${ctx.request.url}`);
next();
};
const main = ctx => {
ctx.response.body = 'hello world';
};
const middlewares = compose([logger,main]);
app.use(middlewares);
app.listen(3000);
9.处理错误的中间件
const Koa = require('koa');
const app = new Koa();
const handler = async (ctx,next) => {
try{
await next();
}catch (err) {
ctx.response.status = err.statusCode || err.status || 500;
ctx.response.body = {
message: err.message
};
ctx.app.emit('error', err, ctx);
}
};
const main = ctx => {
ctx.throw(500);
};
app.on('error', (err, ctx) =>{
console.error('server error', err);
}
);
app.use(handler);
app.use(main);
app.listen(3000);
10.cookie
const Koa = require('koa');
const app = new Koa();
const main = ctx =>{
let n = Number(ctx.cookies.get('views') || 0) + 1;
ctx.cookies.set('views',n);
ctx.response.body = n + ' views';
};
app.use(main);
app.listen(3000);
11.表单提交:koa-body模块
const Koa = require('koa');
const koaBody = require('koa-body');
const app = new Koa();
const main = async ctx => {
let body = ctx.request.body;
console.log(body);
if (!body.name) {
ctx.throw(404,'.name required');
}
ctx.response.body = {name: body.name};
};
app.use(koaBody());
app.use(main);
app.listen(3000);