Home > Article > Web Front-end > 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!