【nodejs】使用response输出中文但页面中文乱码的处置

时间:2020-11-27 13:38:01

两点要确认:

1.head里有<meta charset="utf-8"/>

2.js文件编码为utf-8格式。

第二点往往容易被忽略,所以出现乱码。

【nodejs】使用response输出中文但页面中文乱码的处置

附上代码:

'use strict';

var express=require('express');
var http=require('http');
var app=express();

app.get('/index.html',function(req,rsp){
    rsp.writeHead(200,{'Content-Type':'text/html'});
    rsp.write('<head><meta charset="utf-8"/></head>');
    rsp.write('<body>你好,Express</body>');// 如有中文乱码先确认js文件编码是utf-8格式
    rsp.end();//没有end浏览器会一直转
});
app.listen(8080,"127.0.0.1");