koajs / charset

use iconv-lite to encode the body and set charset to content-type

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

check `content-type` for charset

jonathanong opened this issue · comments

would be cool to be check the .type for the charset, then re-encode properly instead of doing a global charset. for example:

app.use(charset())

app.use(function* (next) {
  var charset = this.acceptsCharsets('utf8', 'gbk') || 'utf8'
  this.vary('accept-charset')
  this.type = 'text/plain; charset=' + charset
  this.body = 'something'
})

then this middleware will automatically re-encode the body only if the charset is not utf8.