GithubHelp home page GithubHelp logo

charset's Introduction

NPM version build status Coveralls David deps node version Gittip

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

Install

# npm ..
npm i koa-charset
# yarn ..
yarn add koa-charset

Usage

const Koa = require('koa');
const charset = require('koa-charset');

const app = new Koa();

app.use(charset());
app.use(function (ctx) {
  ctx.body = '你好';
  ctx.type = 'text/html; charset=gbk';
});

app.listen(3000);

Options

  • charset: set global charset by options.charset

Manually turning charset on and off

You can set ctx.charset to cover the global charset.

app.use(function (ctx) {
  ctx.charset = 'gb2312';
  ctx.body = '你好';
});

You can disable charset by ctx.charset = false.

app.use(function (ctx) {
  ctx.charset = false;
  ctx.body = 'hello';
});

also this module will get the charset from Content-Type, so you can just use koa-charset, then this middleware will automatically re-encode the body only if the charset is not utf8.

const app = new Koa();
app.use(charset());
app.use(function () {
  const charset = ctx.acceptsCharsets('utf8', 'gbk') || 'utf8';
  ctx.vary('accept-charset');
  ctx.type = `text/plain; charset=${charset}`;
  ctx.body = 'something';
});

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.