30

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

name: "John D�m"

Instead of:

name: "John Döm"

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/');
Jack
  • 3,632
  • 7
  • 45
  • 67
  • 3
    All strings in JS are UTF8 unless explicitly set to something else, so it is hard to say where your error would be coming from. What does `driver` do? The code you've provided is standard boilerplate and doesn't really help. – loganfsmyth Apr 28 '13 at 20:21
  • `driver` grabs all drivers from a mongoDB and prints all data out. See link here: http://xn--billstrm-t4a.se:1337/ – Jack Apr 28 '13 at 20:23
  • @loganfsmyth `driver = require('./driver');` Is pretty much the same as this one: https://gist.github.com/ccoenraets/3819468#file-wines-js – Jack Apr 28 '13 at 20:24
  • My guess would be that the data in your DB is not UTF8. How did you populate the DB? – loganfsmyth Apr 28 '13 at 20:40

3 Answers3

49

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

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

Otherwise the browser displays the content in it's favorite encoding.

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

For older node.js versions use:

res.header("Content-Type", "application/json; charset=utf-8");
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
TheHippo
  • 61,720
  • 15
  • 75
  • 100
  • 1
    `res.charset = 'utf-8'` is the same thing, right? I find it cleaner – franzlorenzon Sep 10 '13 at 10:51
  • 2
    res.charset did not work for me (nodejs 0.10.8) but this json-style worked : http://pastebin.com/NwcVEAra – loloof64 May 30 '14 at 10:47
  • Is this answer still correct? ...I'm having a hard time in my code... I keep getting � marks... – D.Tate Oct 12 '15 at 21:40
  • @D.Tate answer is still correct. The 'utf-8' depends on the encoding of your datasource. – TheHippo Oct 13 '15 at 09:30
  • Hi, I do not use a DB and want to avoid using with writeHead but I get `TypeError: res.header is not a function`error. Then solved this problem with this: `res.setHeader('content-type', 'text/html; charset=utf-8');` text/html part is about my code, it works with application/json – eemrah Feb 04 '18 at 15:33
1

I can't solve this problem with setting content type. I solved this problem with encode function.

res.cookie('someCookie', someCookie, {
  encode: c => c,
});

For more information: express cookie

ExpressJS version: 4.16.4

Murat Ersin
  • 428
  • 6
  • 12
-4

My problem solved with this:

res.writeHeader(200 , {"Content-Type" : "text/html; charset=utf-8"});

Ehsan Ali
  • 1,362
  • 5
  • 25
  • 51
  • 2
    This could lead to problems, if you want to return content but an HTTP status code other then `200`. – TheHippo Oct 09 '16 at 13:05