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 });
  • 相关阅读:
    NOIP2011 D1T1 铺地毯
    NOIP2013 D1T3 货车运输 倍增LCA OR 并查集按秩合并
    POJ 2513 trie树+并查集判断无向图的欧拉路
    599. Minimum Index Sum of Two Lists
    594. Longest Harmonious Subsequence
    575. Distribute Candies
    554. Brick Wall
    535. Encode and Decode TinyURL(rand and srand)
    525. Contiguous Array
    500. Keyboard Row
  • 原文地址:https://www.cnblogs.com/fsg6/p/13081866.html
Copyright © 2011-2022 走看看