Node.js/Express-如何设置响应字符编码? [英] Node.js / Express - How do I set response character encoding?

查看:656
本文介绍了Node.js/Express-如何设置响应字符编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我明白了

app.get('/json', function(req, res) {
    res.set({
        'content-type': 'application/json'
    }).send('{"status": "0"}');
});

我正在尝试以UTF-8格式发送响应,但没有成功:

I'm trying to send the response as UTF-8 with the following with no success:

app.get('/json', function(req, res) {
    // From Node.js Official Doc
    // http://nodejs.org/api/http.html#http_http_request_options_callback
    res.setEncoding('utf8');

    res.set({
        'content-type': 'application/json'
    }).send('{"status": "0"}');
});

在Express中设置字符编码的正确方法是什么?

What is the correct way to set character encoding in Express?

推荐答案

使用res.charset: http ://expressjs.com/api.html#res.charset

Use res.charset: http://expressjs.com/api.html#res.charset

res.charset = 'value';
res.send('some html');
// => Content-Type: text/html; charset=value

但是,JSON默认情况下为UTF-8,因此您无需进行任何设置.

However, JSON is UTF-8 by default so you don't need to set anything.

这篇关于Node.js/Express-如何设置响应字符编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆