Merge pull request #66 from u3u/hotfix/utf-8

fix: incorrect `utf-8` characters
This commit is contained in:
DIYgod 2018-05-03 11:10:53 +08:00 committed by GitHub
commit f3d028be48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 12 deletions

View File

@ -5,6 +5,7 @@ const config = require('./config');
const onerror = require('./middleware/onerror');
const header = require('./middleware/header.js');
const utf8 = require('./middleware/utf8');
const memoryCache = require('./middleware/cache.js');
const redisCache = require('koa-redis-cache');
@ -24,22 +25,28 @@ app.use(onerror);
// set header
app.use(header);
// fix incorrect `utf-8` characters
app.use(utf8);
// cache
if (config.cacheType === 'memory') {
app.use(memoryCache({
expire: config.cacheExpire
}));
}
else if (config.cacheType === 'redis') {
app.use(redisCache({
expire: config.cacheExpire,
onerror: (e) => {
logger.error('cache error', e);
}
}));
app.use(
memoryCache({
expire: config.cacheExpire
})
);
} else if (config.cacheType === 'redis') {
app.use(
redisCache({
expire: config.cacheExpire,
onerror: (e) => {
logger.error('cache error', e);
}
})
);
}
// router
app.use(router.routes()).use(router.allowedMethods());
app.listen(config.port);
app.listen(config.port);

6
middleware/utf8.js Normal file
View File

@ -0,0 +1,6 @@
// https://stackoverflow.com/questions/2507608/error-input-is-not-proper-utf-8-indicate-encoding-using-phps-simplexml-lo/40552083#40552083
// https://stackoverflow.com/questions/1497885/remove-control-characters-from-php-string/1497928#1497928
module.exports = async (ctx, next) => {
await next();
ctx.body = ctx.body.replace(/[\x00-\x1F\x7F]/g, '');
};