dongyu 50bcadf73e migrate to koa@2 (#108)
* support async/await

* change nodejs version 4 or 6 to 5.6

* eslint

* remove jade
2017-05-30 20:44:57 +08:00

36 lines
672 B
JavaScript

const Koa = require('koa');
const fs = require('fs');
const app = module.exports = new Koa();
const path = require('path');
const extname = path.extname;
// try GET /app.js
app.use(async function(ctx) {
const fpath = path.join(__dirname, ctx.path);
const fstat = await stat(fpath);
if (fstat.isFile()) {
ctx.type = extname(fpath);
ctx.body = fs.createReadStream(fpath);
}
});
if (!module.parent) app.listen(3000);
/**
* thunkify stat
*/
function stat(file) {
return new Promise(function(resolve, reject) {
fs.stat(file, function(err, stat) {
if (err) {
reject(err);
} else {
resolve(stat);
}
});
});
}