javascript - How to read files in koa
習慣沉默
習慣沉默 2017-05-16 13:36:38
0
2
780

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

習慣沉默
習慣沉默

reply all(2)
曾经蜡笔没有小新

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);
漂亮男人

koa-sendfile

koa 好像没有直接类似 expressres.sendFile() Such syntax requires adding middleware

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template