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

青灯夜游
Release: 2020-11-17 18:12:28
forward
2093 people have browsed it

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("启动成功")
});
Copy after login

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'});
Copy after login
'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("启动成功")
});
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template