zoukankan      html  css  js  c++  java
  • request请求头的数据

     1 //1. 引入 HTTP 模块
     2 const http = require("http");
     3 //引入 url 模块
     4 const urlTool = require("url");
     5 
     6 //2. 创建服务对象
     7 const server = http.createServer((request, response) => {
     8     //得到请求报文的数据
     9     //1. 请求方法的获取
    10     // console.log(request.method); //会有两次结果
    11     //2. 请求报文的 URL
    12     // console.log(request.url);//向服务器发送请求,如果没有设定任何的路径,则默认的路径是/
    13     //3. HTTP 协议版本的获取
    14     // console.log(request.httpVersion);
    15     //4. 获取报文的请求信息
    16     // console.log(request.headers);
    17     // console.log(request.headers.host);
    18     // console.log(request.headers['cache-control']);
    19     //5. 获取 URL 的路径部分 和 查询字符串部分
    20     //url获取的是一个对象,里头的属性是字符串,第二个参数true,将对象的属性变成一个对象
    21     // console.log(urlTool.parse(request.url, true));
    22     let url = urlTool.parse(request.url, true);  //将request.url拆分成两部分,url路径,查询字符串
    23     //获取查询参数 a
    24     //query是保存的查询字符串
    25     let a = url.query.a;
    26     //获取路径字符串
    27     //pathname保存的路径
    28     let path = url.pathname;
    29     console.log(a);
    30     console.log(path);
    31 
    32     //设置响应体
    33     response.end("server is running");
    34 });
    35 
    36 //3. 启动服务
    37 server.listen(8000, ()=>{
    38     console.log("服务已经启动, 端口 8000 监听中.....");
    39 });
  • 相关阅读:
    构建之法阅读笔记01
    最长英语单词链
    第十五周学习总结
    寻找“水王”
    Happy Necklace HDU
    Bi-shoe and Phi-shoe LightOJ
    The Embarrassed Cryptographer POJ
    Big Number HDU
    矩阵乘法模板C/C++
    Baby Step,Giant Step算法模板
  • 原文地址:https://www.cnblogs.com/fsg6/p/13081866.html
Copyright © 2011-2022 走看看