CNode

Nodejs 编码方式的设定问题

问答
Ttianmen发布于14 年前最后回复11 年前6 回复33165 浏览0 收藏

用Nodejs+express写了一个服务器程序:

var express = require('express'); 
var app = express.createServer(); 
var io = require('socket.io').listen(app); 
app.configure(function () { 
....//此处代码省略
}); 

app.get('/', function (req, res) { 
res.sendfile(__dirname + '/public/index.html'); }); 
app.use('/public', express.static(__dirname + '/public')); 
app.listen(7000); 
console.log('daemon start on http://127.0.0.1:7000');

做了一个 index.html 页面,在本地用IE直接打开OK, 用 http://127.0.0.1:7000 访问成乱码

想加入 encoding = ‘gb2312', 在哪里修改比较好?

查看回复

回复 (6)

T
tomoya92#1·11 年前

我现在写代码必选utf-8其他都不用

M
myy#2·11 年前

同意楼上,,楼主正确的做法是,将 index.html 另存为 utf8 编码。。

J
jiyinyiyong#3·14 年前

楼上的 Markdown 格式... 看晕了
编码应该可以写在 index.html 里吧?

F
fish#4·14 年前

res 设置 输出http heder信息:

res.setHeader('content-type', 'text/html;charset=gb2312');

或者在模板中加入:

<meta http-equiv="content-type" content="text/html;charset=gb2312">
T
tianmen#5·14 年前

谢谢fish的回复, 后来加入: res.charset = 'gb2312'; 搞定了

F
fengmk2#6·14 年前

还gb2312?以后会遇到各种问题的。

参与回复
登录后即可参与回复。登录