koa-plugins/plugins/static/index.js
2020-03-01 17:38:34 +01:00

25 lines
No EOL
625 B
JavaScript

/**
* Static pages
*/
const fs = require("fs");
const path = require("path")
const PassThrough = require('stream').PassThrough;
module.exports = {
enabled: true,
exec(ctx){
var p = path.join(__dirname, "../../contents", ctx.request.url);
if(!fs.existsSync(p)){
ctx.body = "Not Found";
ctx.status = 404;
return;
}
ctx.set('Content-Type', 'text/html');
ctx.body = fs.createReadStream(p).on('error', ctx.onerror).pipe(PassThrough());
},
rules(ctx){
return ctx.request.url.split('.').pop() != "php";
},
priority: -5
}