Home >Web Front-end >JS Tutorial >A brief discussion on the solution to Chinese garbled characters in node

A brief discussion on the solution to Chinese garbled characters in node

青灯夜游
青灯夜游forward
2020-11-17 18:12:282107browse

A brief discussion on the solution to Chinese garbled characters in node

Related recommendations: "nodejs Tutorial"

When I was learning node today, I was following the code in the video, but Chinese appeared. In the case of garbled code, the Google Chrome in the video may be inconsistent with my version. Let’s look at the code first:

'use strict';
const http = require("http");
let count = 0;
const server = http.createServer((req, res) => {
    res.write(`这是第${count++}个访问的`);
    res.end();
});
server.listen(2080, error => {
    if (error)
        throw error;
    console.log("启动成功")
});

I want to use node to build a local server and then count the number of visits, but it appears Chinese garbled characters:

杩欐槸绗�0涓闂殑

后来查询资料,原来加一个头部代码就行:
设置 res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
'use strict';
const http = require("http");
let count = 0;
const server = http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
    res.write(`这是第${count++}个访问的`);
    res.end();
});
server.listen(2080, error => {
    if (error)
        throw error;
    console.log("启动成功")
});

For more programming-related knowledge, please visit: Programming Learning Website! !

The above is the detailed content of A brief discussion on the solution to Chinese garbled characters in node. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete