nodejs 服务器http模块使用和Content-Type

http 模块主要用于搭建 HTTP 服务端和客户端

使用 HTTP 服务器或客户端功能必须调用 http 模块

//引入http模块
let http = require("http")
let fs = require("fs")


//创建web服务器   request和response都是流的实例
let server = http.createServer(function(request,response){
    //创建写入流  设置响应头 (状态码,内容类型)
    response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'})

    //读取html文件
    var html = fs.readFileSync('./index.html','utf8')

    //创建写入流  
    // response.write('<h1>这里是nodejs</h1>')
    response.write(html.toString())

    response.end('<h1>这里是nodejs的end()</h1>')

    //通过流传输
    //创建读取流
    //var myReadStream = fs.createReadStream(__dirname +'/index.html','utf8')
    //通过管道写入到response 传输到浏览器
    //myReadStream.pipe(response)
})

//监听在端口8888
server.listen(8888,'127.0.0.1')

console.log('服务启动端口8888')


/*
Content-Type
    MediaType,即是Internet Media Type,互联网媒体类型;也叫做MIME类型,在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息

    常见的媒体格式类型
        text/html--> HTML格式
        text/plain-->纯文本格式      
        text/xml-->  XML格式
        image/gif-->gif图片格式    
        image/jpeg-->jpg图片格式 
        image/pn-->png图片格式

    application开头的媒体格式类型
        application/xhtml+xml-->XHTML格式
        application/xml    --> XML数据格式
        application/atom+xml -->Atom XML聚合格式    
        application/json   --> JSON数据格式
        application/pdf      -->pdf格式  
        application/msword --> Word文档格式
        application/octet-stream--> 二进制流数据(如常见的文件下载)
        application/x-www-form-urlencoded--> <form encType="">中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)
    
    上传文件时使用
        multipart/form-data--> 需要在表单中进行文件上传时,就需要使用该格式
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xuhang139

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值