koa2怎么指定html跳转首页?
在express4.*是这样实现的:
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res){
res.sendFile(__dirname+'/index.html');
});
http.listen(5566, function(){
console.log('listening on *:5566');
});
变成koa2 应该怎么写? 网上的教程都是只有这样:
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx, next) => {
await next();
ctx.response.type = 'text/html';
ctx.response.body = '<h1>Hello, koa2! to cmy</h1>';
});
app.listen(9000);
console.log('app started at port 9000...');
读取文件直接fs.readFile,将返回文件赋给ctx.body,不就行了!注意设置ctx.type为html,否则就是下载了!