utf8 code support

1 view
Skip to first unread message

816...@gmail.com

unread,
Dec 26, 2009, 5:57:48 AM12/26/09
to nodejs
if I use utf8 symbols in my code? for example:
---------------------------
var sys = require('sys'),http = require('http');
http.createServer(function (req, res) {
res.sendHeader(200, {'Content-Type': 'text/plain'});
res.sendBody('абвгдеёжзиклмнопрст ');
res.finish();
}).listen(8000);
sys.puts('Server running at http://127.0.0.1:8000/');
---------------------------
Node thinks very long before any answer (about 30 sec)
after that returns something like:
---------------------------
012345Q678:;<=>?@AB
0
---------------------------
If I use only English characters, no problem occupied. =(

Daniel N

unread,
Dec 26, 2009, 8:20:14 AM12/26/09
to nod...@googlegroups.com

--

I found this one the other day.  When you send the body, you have to specify that you're sending utf8.

res.sendBody("café", "utf8")

I think that should do it.

~ Daniel
 

You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.



Micheil Smith

unread,
Dec 26, 2009, 8:34:08 AM12/26/09
to nod...@googlegroups.com
There was something in the code that ascii or something was the assumed default, so yes, you'd need to try setting the sendBody to use utf8, as Daniel says.

- Micheil

> --

816...@gmail.com

unread,
Dec 26, 2009, 9:11:45 AM12/26/09
to nodejs
Thanks very much, it helped!

Irfan Shah

unread,
Dec 26, 2009, 7:32:41 AM12/26/09
to nod...@googlegroups.com
Hi Oleg,
You should use 
       res.sendBody('абвгдеёжзиклмнопрст ', encoding ='utf8');
to fix this problem.


cheers,
-irfan


Irfan Shah

unread,
Dec 26, 2009, 7:40:57 AM12/26/09
to nod...@googlegroups.com
you can also hint the browser by setting the appropriate header ( charset=utf-8 )

var sys = require('sys'),http = require('http');

http.createServer(function (req, res) {
       res.sendHeader(200, {'Content-Type': 'text/plain; charset=utf-8'});
       res.sendBody('абвгдеёжзиклмнопрст ', encoding ='utf8');
       res.finish();
}).listen(1234);
sys.puts('Server running at http://127.0.0.1:1234/');


cheers,
-irfan

Isaac Z. Schlueter

unread,
Dec 26, 2009, 11:25:18 PM12/26/09
to nodejs
On Dec 26, 4:32 am, Irfan Shah <mail.u...@gmail.com> wrote:
>        res.sendBody('абвгдеёжзиклмнопрст ', encoding ='utf8');

JavaScript isn't Python. This style just leaks a global symbol
unnecessarily, and provides no benefit.

Do it this way instead:
res.sendBody('абвгдеёжзиклмнопрст ', 'utf8');

Note the lack of "encoding=". If you want to indicate the meaning of
the parameter, you can use a comment.

Thanks!

--i

Jed Schmidt

unread,
Dec 26, 2009, 11:32:15 PM12/26/09
to nod...@googlegroups.com
On Sat, Dec 26, 2009 at 11:25 PM, Isaac Z. Schlueter <i...@foohack.com> wrote:
> On Dec 26, 4:32 am, Irfan Shah <mail.u...@gmail.com> wrote:
>> res.sendBody('абвгдеёжзиклмнопрст ', encoding ='utf8');
>
> JavaScript isn't Python. This style just leaks a global symbol
> unnecessarily, and provides no benefit.

I think Irfan may have actually just copied that from the node API docs.

This is how it appears there:

> response.sendBody(chunk, encoding="ascii")

(perhaps there's a less confusing way of indicating the intent of an argument?)

Jed Schmidt

Jeff Smick

unread,
Dec 27, 2009, 12:08:14 AM12/27/09
to nod...@googlegroups.com
Syntactically you can't write "response.sendBody(chunk,
encoding="ascii")" in javascript so wouldn't it be obvious that the
intent is to say that the second argument is "encoding" and it
defaults to "ascii?"

Jed Schmidt

unread,
Dec 27, 2009, 12:17:24 AM12/27/09
to nod...@googlegroups.com
On Sun, Dec 27, 2009 at 12:08 AM, Jeff Smick <sprs...@gmail.com> wrote:
> Syntactically you can't write "response.sendBody(chunk,
> encoding="ascii")" in javascript so wouldn't it be obvious that the
> intent is to say that the second argument is "encoding" and it
> defaults to "ascii?"

Actually, I'm pretty sure that's valid Javascript, as is this:

> console.log( 123, a = 456, a )

Though it might be better to use comments to indicate defaults:

> response.sendBody(chunk, encoding /* ("ascii") */ )

There must be a convention somewhere that has this, maybe in the dojo
codebase? Either way, since it doesn't break anything, I don't see it
as a huge issue.

Jed Schmidt

Jeff Smick

unread,
Dec 27, 2009, 2:08:40 AM12/27/09
to nod...@googlegroups.com
Oh right. It'd just assign "ascii" to "encoding." My mistake.

Serge in Darkness

unread,
Dec 28, 2009, 7:33:25 AM12/28/09
to nodejs
Shouldn't it be the other way? ECMAScript standard (ECMA-262 p.6)
states that source code is Unicode / UTF-16, so I presume default
expected encoding in function parameters should be UTF.

Connor Dunn

unread,
Dec 28, 2009, 10:08:17 AM12/28/09
to nod...@googlegroups.com
I believe the reason is utf-8 is often not required and is slower. You
are also giving the encoding the data should be transmitted as, not
the encoding it is in the source, if you specify binary as the
encoding I believe it will send the utf-16 characters.

2009/12/28 Serge in Darkness <bolte...@gmail.com>:

Tim Caswell

unread,
Dec 28, 2009, 11:13:02 AM12/28/09
to nod...@googlegroups.com
I've also noticed that node as a general rule errs on the side of performance over safety when the two are at odds with each other. Yes it would be safer to default to unicode so that people unaware with encoding have text that just works, but slower.

I would call the default to ascii and the obvious documentation's bias against unicode "premature optimization", but the node about statement clearly states that a benefit of node is that "less-than-expert programmers are able to develop fast systems." So I think this was a design choice.

I personally would have preferred siding with default unicode, and explicit ascii for faster code when you know it's safe, but in the end it's really not a big deal either way. Once it bites you, it's easy to fix.

Reply all
Reply to author
Forward
0 new messages