koa基础实践

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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值