const fs = require('fs') module.exports = function(app, conf) { app.use(async (ctx, next) => { await next() if (ctx.path === '/') { let html = await handleDict(ctx.body) ctx.body = html } }) } function handleDict (buffer) { return new Promise((resolve, reject) => { let rawHTML = '' if (buffer instanceof Buffer) { rawHTML = replacePlaceholder(buffer.toString('utf-8')) resolve(rawHTML) } else { buffer.on('data', data => rawHTML += replacePlaceholder(data.toString())) buffer.on('end', data => resolve(rawHTML)) buffer.on('error', e => reject(e)) } }); } function replacePlaceholder (str) { return str .replace('{{version}}', `?version=${ (Math.random() * 1000000).toFixed(0) }`) .replace('{{lang}}', 'zh') }