Beginner to koa, using ctx in app.use to directly return the html string can be displayed, but when using fs.readFile and assigning data to ctx.body in the callback, not found is displayed on the browser. What should I do? How to write it, I couldn’t find
曾经蜡笔没有小新2017-05-16 13:38:38
ReadFile directly is asynchronous, right? Use readFileSync?
Or refer to this?
var app = require('koa')();
var fs = require('fs');
app.use(function *(){
this.body = yield new Promise(function(reso,reje){
fs.readFile(__dirname+'/app.html',function(err,data){
if(err)
reso('error');
else
reso(data.toString());
})
}).then(function(data){
return data
});
})
app.listen(8910);
漂亮男人2017-05-16 13:38:38
koa-sendfile
koa
好像没有直接类似 express
的 res.sendFile()
Such syntax requires adding middleware