Node.js / Express - 如何设置响应字符编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17872789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js / Express - How do I set response character encoding?
提问by gsklee
Say I got:
说我得到:
app.get('/json', function(req, res) {
res.set({
'content-type': 'application/json'
}).send('{"status": "0"}');
});
I'm trying to send the response as UTF-8 with the following with no success:
我正在尝试将响应作为 UTF-8 发送,但没有成功:
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"}');
});
What is the correct way to set character encoding in Express?
在 Express 中设置字符编码的正确方法是什么?
采纳答案by Dan Kohn
Use res.charset: http://expressjs.com/api.html#res.charset
使用 res.charset:http://expressjs.com/api.html#res.charset
res.charset = 'value';
res.send('some html');
// => Content-Type: text/html; charset=value
However, JSON is UTF-8 by default so you don't need to set anything.
但是,JSON 默认为 UTF-8,因此您无需设置任何内容。
回答by Iain Collins
You will probably want to explicitly add a charset to the end of your content-type string if you find it's not being set already by Express:
如果您发现 Express 尚未设置字符集,您可能希望在内容类型字符串的末尾显式添加一个字符集:
res.set({ 'content-type': 'application/json; charset=utf-8' });
The charset is notalways set automagically and does need to be setto work correctly everywhere (i.e. with all browsers and all ajax libraries) or you can run into encoding bugs.
字符集并不总是自动设置的,并且需要设置为在任何地方都能正常工作(即所有浏览器和所有 ajax 库),否则您可能会遇到编码错误。
In Express 4.x specifically I've found that depending on the object you trying to return, it normallyautomatically returns with content-type: application/json; charset=utf-8when you call res.json(someObject), however not always.
特别是在 Express 4.x 中,我发现根据您尝试返回的对象,它通常会content-type: application/json; charset=utf-8在您调用 时自动返回res.json(someObject),但并非总是如此。
When calling res.json()on some objects it can return content-type: application/json(i.e. without the charset encoding!). I'm not actually sure what triggers this, other than it's something about the specific object being returned.
当调用res.json()某些对象时,它可以返回content-type: application/json(即没有字符集编码!)。我实际上不确定是什么触发了这个,除了它是关于被返回的特定对象的东西。
I've only noticed it because of automated tests which explicitly checked the headers and found it was missing the charset declaration on some responses (even though the content-type was still application/json).
我只注意到它是因为自动测试显式检查了标题并发现它在某些响应中缺少字符集声明(即使内容类型仍然是 application/json)。
回答by Moronicsmurf
Having similar issues I'm collecting Swedish characters from a database and outputting them as JSON object, node doesn't really care if json must be UTF-8 or not when the chars from the database isn't in UTF-8.. So assuming "you don't need to set anything" is false. Depending on what charsets you are working with.
遇到类似的问题,我从数据库中收集瑞典语字符并将它们作为 JSON 对象输出,当数据库中的字符不是 UTF-8 时,节点并不真正关心 json 是否必须是 UTF-8 .. 所以假设“你不需要设置任何东西”是错误的。取决于您正在使用的字符集。
回答by Siva
This worked for me
这对我有用
res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
回答by Ronnie Royston
Before you go to the trouble of manually setting header parameters, check what your server is already sending by default. In my case, I'm using a "serverless" cloud provided Node.js instance. Apparently, these are usually front-ended w/ NGINX which I assume is what sets some of this stuff based on default settings. ...I didn't need to res.setanything at all. Granted, I'm serving back HTML, ...just sayin - before you go fixin, make sure it's broke.
在您手动设置标头参数的麻烦之前,请检查您的服务器默认已发送的内容。就我而言,我使用的是“无服务器”云提供的 Node.js 实例。显然,这些通常是带有 NGINX 的前端,我认为这是基于默认设置设置这些东西的原因。......我根本不需要res.set任何东西。当然,我正在提供 HTML,......只是说 - 在你修复之前,确保它坏了。
accept-ranges: bytes
accept-ranges: bytes
cache-control: private
content-encoding: gzip
content-type: text/html; charset=utf-8
date: Fri, 21 Dec 2018 21:40:37 GMT
etag: W/"83-xwilN/BBLLLAAAHHH/0NBLAH0U"
function-execution-id: 5thvkjd4wwru
server: nginx
status: 200
vary: accept-encoding, cookie, authorization
via: 1.1 varnish
x-cache: MISS
x-cache-hits: 0
x-cloud-trace-context: 18c611BBBBLLLLAAAHHH9594d9;o=1
x-powered-by: Express
x-served-by: cache-dfw18631-DFW
x-timer: S15BBLLLAAHHH.913934,VS0,VE3404

