(1)使用
基本用法:
const Koa = require('koa');
const app = new Koa();
app.use(async ctx => {
ctx.response.type = 'html';
ctx.response.body =
fs.createReadStream('./view/include/test.html');
});
配置路由:
if(ctx.request.path === '/') {
ctx.response.type = 'html';
ctx.response.body = '<div>根目录</div>';
}else {
ctx.response.body = '哈哈哈,随机路径';
}
使用koa-router模块:
const Koa = require('koa');
const route = require('koa-route');
const app = new Koa();
var main = ctx => {
ctx.response.type = 'html';
ctx.response.body = '<h1>about<h1>';
}
app.use(route.get('/about'), main);
静态资源: // 直接访问浏览器js代码 可查看
const serve = require('koa-static');
const app = new Koa();
const _static = serve(path.join(__dirname));
app.use(_static);
重定向:
const redirect = ctx => {
ctx.response.redirect('/');
};
app.use(route.get('/redirect', redirect));
抛错:
<