如何在Node.js中获取UTF-8? [英] How to get UTF-8 in Node.js?

查看:105
本文介绍了如何在Node.js中获取UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的API上获得UTF-8支持?
现在,字符串输出如下:

How do I get UTF-8 support on my API? At the moment, a string outputs like this:

name: "John D�m"

而不是:

name: "John Döm"

Checkout app.js如下:

Checkout app.js below:

var express = require('express'),
    driver = require('./driver');

var app = express();

app.configure(function () {
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
});

app.get('/drivers', driver.findAll);

app.listen(3000);
console.log('Up: http://127.0.0.1:3000/');


推荐答案

挂钩到响应生成器或创建一个中间件

Hook into you response generator or create a middleware that does the following:

res.header("Content-Type", "application/json; charset=utf-8");

否则浏览器会以他最喜欢的编码显示内容。

Otherwise the browser displays the content in his favorite encoding.

如果这不能帮助你DB可能是错误的编码。

If this doesn't help you DB is probably in the wrong encoding.

这篇关于如何在Node.js中获取UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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