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--> 需要在表单中进行文件上传时,就需要使用该格式
*/