当前位置:首页 » 《随便一记》 » 正文

4Http模块、Url模块_quanxiaobai_的博客

8 人参与  2021年12月24日 15:58  分类 : 《随便一记》  评论

点击全文阅读


  • 如果我们使用PHP来编写后端的代码时,需要Apache或者Nginx的HTTP 服务器,来处理客户端的请求相应。不过对于Node.js来说,概念完全不一样了。使用Node.,js时,我们不仅仅在实现一个应用,同时还实现了整个HTTP服务器。
  • 我们创建NodeJs项目的时候,路径不要有中文和空格
  • 开发工具建议使用vscode,然后我们安装nodejs的提示插件:Node Snippets

 

 

 

 

测试一下node的http服务器

app.js

//导入http模块
const http = require('http');

/*
    (req,res)=>{}是一个匿名函数,跟function(req,res){}一样其实,只不过这这是ES6的写法罢了
 */
http.createServer((req,res)=>{

    console.log(req.url);//打印以下请求看看

    //设置响应头
    //状态码200,文件类型html,字符集utf-8
    res.writeHead(200,{"Content-type":"text/html;charset='utf-8'"});
    res.write('my node.js');
    res.end();//结束响应。一定要结束响应,不然客户端会一直加载。
}).listen(3000);//监听3000端口

 

命令行运行app.js

node app.js

 

浏览器输入:localhost:3000
得到以下页面
在这里插入图片描述

 

用Chrome进行调试查看发送了多少个请求

  • 终端打印的结果
    在这里插入图片描述
  • 第一个请求
    在这里插入图片描述
  • 第二个请求(说明一下,这个请求是请求网页开头那个图标的)
    在这里插入图片描述

 

 

 

 

url模块

  • url模块可以用来解析url

下面演示一下url模块

const url = require('url');

var api = 'https://www.baidu.com?name=zhangsan&age=21';

// console.log(url.parse(api));
/*
    Url {
  protocol: 'https:',   
  slashes: true,        
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: '?name=zhangsan&age=21',
  query: 'name=zhangsan&age=21',
  pathname: '/',
  path: '/?name=zhangsan&age=21',
  href: 'https://www.baidu.com/?name=zhangsan&age=21'
}
 */

//我们再传递一个参数true,即可把query封装成一个对象
// console.log(url.parse(api,true));
/*
    Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: '?name=zhangsan&age=21',
  query: [Object: null prototype] { name: 'zhangsan', age: '21' },
  pathname: '/',
  path: '/?name=zhangsan&age=21',
  href: 'https://www.baidu.com/?name=zhangsan&age=21'
}
*/
var temp = url.parse(api,true).query;
console.log(temp);//[Object: null prototype] { name: 'zhangsan', age: '21' }
console.log(temp.name);//zhangsan
console.log(temp.age);//21

 

下面演示在实际node的http服务器中如何使用url模块

//导入http模块
const http = require('http');
//导入url模块
const url = require('url');

http.createServer((req,res)=>{

    console.log(req.url);//打印以下请求看看

    //设置响应头
    //状态码200,文件类型html,字符集utf-8
    res.writeHead(200,{"Content-type":"text/html;charset='utf-8'"});

    //打印一下req里面有什么
    // console.log(req);
    /*
        req里面有很多很多东西,其中就包括了url
    */
   
    //其中url又有两条(实际开发中会有很多种情况),我们过滤掉这条
    if(req.url != '/favicon.ico'){
        var query = url.parse(req.url,true).query;
        /*
            query = JSON.parse(temp);
            这里如果用JSON.parse的话它会报错,原因是因为传了个undefined,但明明没有传undefined
            我也不知道... ... 
        */
        console.log(query.name);//zhangsan
    }
    // console.log(temp);//[Object: null prototype] { name: 'zhangsan' }
    /*
        https://blog.csdn.net/weixin_44710964/article/details/103552999
        当出现[Object: null prototype]我们可以先对对象进行JSON字符串转化(JSON.stringify()),
        然后再转化成对象(JSON.parse()),这样就可以去除了(以下方法对遇到[Object: null prototype]都通用)
    */
    
    //晕,好像还是不行

    res.end();//结束响应。一定要结束响应,不然客户端会一直加载。
}).listen(3000);//监听3000端口

实际上这种方法官方文档里面已经列为废弃的方法了,所以会有一些问题,下面介绍最新的方法。

 

参考:https://blog.csdn.net/weixin_44710964/article/details/103552999

//导入url模块
const url = require('url');

//node官方文档最新处理url方法
var obj = new URL('http://www.zcq.com/fafe?name=zhangsan&age=18');//前面一定要http://或https://不然报错
// console.log(obj);
/*
    URL {
  href: 'http://www.zcq.com/fafe?name=zhangsan&age=18',
  origin: 'http://www.zcq.com',
  protocol: 'http:',
  username: '',
  password: '',
  host: 'www.zcq.com',
  hostname: 'www.zcq.com',
  port: '',
  pathname: '/fafe',
  search: '?name=zhangsan&age=18',
  searchParams: URLSearchParams { 'name' => 'zhangsan', 'age' => '18' },
  hash: ''
}
*/

// console.log(obj.searchParams);//URLSearchParams { 'name' => 'zhangsan', 'age' => '18' }

//获取到name和age
console.log(obj.searchParams.get('name'));//zhangsan
console.log(obj.searchParams.get('age'));//18

点击全文阅读


本文链接:http://zhangshiyu.com/post/32083.html

模块  请求  响应  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1