node.js输出网页后乱码

var http = require("http");//负责创建web服务器,处理http相关任务

fs = require('fs');

function load_album_list (callback){

console.log(2);

fs.readdir(

'img','utf-8',function(err,files){

if(err){

callback(err);

return;

}

callback(null,files);

}

);

}

function handle_incoming_request(req,res){

console.log(1);

res.setCharacterEncoding("utf-8"); 

console.log("incoming request:" + req.method+ "" +req.url);

load_album_list(function(err,albums){

if(err){

res.writeHead(503,{"Content-Type":"application/json;charset=utf-8"});

res.end(JSON.stringify(err) + "\n");

return;

}

var out = {

error:null,

data:{albums:albums}

};

res.writeHead(200,{"Content-Type":"application/json"});

res.end("封测人"+JSON.stringify(out) + "\n");

});

}

var s = http.createServer(handle_incoming_request);

s.listen(3000);


zetro
浏览 2191回答 1
1回答

叠加无限

res.end("封测人"+JSON.stringify(out) + "\n");你这里输出的只是html的body里的内容,没有设置<meta charset="utf-8">,改成res.end('<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>'+JSON.stringify(out)+'</body></html>')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Node.js