Method: 1. Use the "res.write("
")" statement; 2. Use "res. setHeader()" or "res.writeHeader()" sets the Content-type type.
The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.
Nodejs method of setting encoding
1. Use res.write() to set the meta tag
res.write("<head><meta chartset='uft-8'/></head>")
2. Use res.setHeader() or res.writeHeader() to set Content-type
res.setHeader("Content-type","text/html;charset=utf8"); res.writeHeader(200,{"Content-type":"text/html;charset=utf8"})
Instructions: Content-Type
Content-Type (content type) generally refers to the Content-Type that exists in a web page. It is used to define the type of network files and the encoding of the web page. It determines in what form and what encoding the browser will read the file. This is often I see that the result of clicking on some PHP web pages is to download a file or an image.
The Content-Type header tells the client the content type of the actual content returned.
Grammar format:
Content-Type: text/html; charset=utf-8 Content-Type: multipart/form-data; boundary=something
[Recommended learning: "nodejs tutorial"]
The above is the detailed content of How to set encoding using nodejs. For more information, please follow other related articles on the PHP Chinese website!