utf8
编码的内容utf8
编码的内容gbk
// require
// 端口号
var http = require('http')
var server = http.createServer()
server.on('request', function (req, res) {
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.end('hello 世界')
server.listen(3000, function () {
console.log('Server is running...')
})
Content-Type
就是用来告知对方我给你发送的数据内容是什么类型 res.setHeader('Content-Type','text/plain; charset=utf-8')
测试:
var http = require('http')
var server = http.createServer()
server.on('request',function(req,res){
res.setHeader('Content-Type','text/plain; charset=utf-8')
res.end('hello 世界!')
})
server.listen(3000,function(){
console.log('Server is running...');
})
text/plain
就是普通文本html
格式的字符串,则也要告诉浏览器我给你发送是text/html
格式的内容var http = require('http')
var server = http.createServer()
server.on('request', function (req, res) {
var url = req.url
if (url === '/plain') {
// text/plain 就是普通文本
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.end('hello 世界')
} else if (url === '/html') {
// 如果你发送的是 html 格式的字符串,则也要告诉浏览器我给你发送是 text/html 格式的内容
res.setHeader('Content-Type', 'text/html; charset=utf-8')
res.end('<p>hello html <a href="">点我</a></p>')
}
})
server.listen(3000, function () {
console.log('Server is running...')
})
localhost:3000/plain
localhost:3000/html
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有