码迷,mamicode.com
首页 > Web开发 > 详细

node.js响应,输出文本解决乱码问题

时间:2020-03-31 14:25:51      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:==   启动   code   func   ext   res   http   包含   serve   

res.setHeader(‘Content-Type‘,‘text/plain;charset="utf-8"‘)

Content-Type的类型

text/plain  文本

text/html  包含html标签的文本

例子:

var http = require(‘http‘)
var server = http.createServer()
server.on(‘request‘,function(req,res){
    if(req.url === ‘/text‘){
        res.setHeader(‘Content-Type‘,‘text/plain;charset="utf-8"‘)
        res.end(‘输出中文不乱码‘)
    }else{
        res.setHeader(‘Content-Type‘,‘text/html;charset="utf-8"‘)
        res.end(‘<h3>标题</h3>‘)
    }
})

server.listen(3000,function(){
    console.log(‘启动成功‘);
    
})

 

node.js响应,输出文本解决乱码问题

标签:==   启动   code   func   ext   res   http   包含   serve   

原文地址:https://www.cnblogs.com/luguankun/p/12604562.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!