mirror of
https://github.com/koajs/examples.git
synced 2026-01-18 14:38:19 +00:00
36 lines
672 B
JavaScript
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);
|
|
}
|
|
});
|
|
});
|
|
}
|