【模块】【通信】---http模块中req和res  常用的属性介绍

const http = require("http");
const fs  = require("fs");
let server = http.createServer((req,res)=>{
    console.log(req.url);
    console.log(req.method);
    console.log(req.headers);
    fs.readFile("./index.html",(err,data)=>{
        
        res.writeHead(200,{'content-type':'text/html;charset=utf8'});
        res.end(data)
    })
    // res.statusCode = 200;
    // res.setHeader("content-type","text/plain")
    
    // res.write('123');
    // res.end('你好');
})


/*
    req:request   请求 
        常用的属性
        req.url     请求地址
        req.method  请求方式
        req.headers 请求头


        ajax({
            type:
            data:
            url:
            headers:{
                content-type:""
            },
            success:
        })


        常见的请求头中的类型
            1、application/json
            2、application/x-www-form-urlencode  数据序列化
                key=val&key=val
        



    res:response  响应

        常用的属性
            res.write 响应 多次
            res.end 结束的响应  一次 
            res.writeHead()  设置响应头与状态码 (下面2个的综合写法)
            res.statusCode   设置状态码
            res.setHeader()  设置响应头


    响应头中content-type常用的类型有哪些?
        1、文本类型   text/plain
        2、html类型   text/html
        3、css类型    text/css
        4、js类型     application/x-javascript
        5、json类型   application/json
        6、图片类型   image/png  image/jpg .....
*/


server.listen(9088)

 

相关文章:

  • 2021-12-16
  • 2022-01-22
  • 2021-07-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-26
  • 2022-12-23
  • 2021-06-24
  • 2021-10-27
  • 2021-05-31
相关资源
相似解决方案