一、http 模块

http.js

const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((request, response) => {
response.statusCode = 200;
response.setHeader('Content-Type', 'text/plain;charset=utf-8');
response.end('http 模块。');
});
server.listen(port, hostname, () => {
console.log(`服务器运行在 http://${hostname}:${port}`);
});

执行 node http.js,访问:127.0.0.1:3000/

二、url 模块

url.js

const url = require('url');
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((request, response) => {
// 过滤掉 request.url == '/favicon.ico' 的情况,否则会打印两次结果
if (request.url != '/favicon.ico') {
console.log(url);
};
response.statusCode = 200;
response.setHeader('Content-Type', 'text/plain;charset=utf-8');
response.end('url 模块。');
});
server.listen(port, hostname, () => {
console.log(`服务器运行在 http://${hostname}:${port}`);
});

  

执行 node url.js,访问:127.0.0.1:3000/

{ parse: [Function: urlParse],
resolve: [Function: urlResolve],
resolveObject: [Function: urlResolveObject],
format: [Function: urlFormat],
URL:
{ [Function: URL]
originFor: [Function],
domainToASCII: [Function],
domainToUnicode: [Function] },
Url: [Function: Url] }

  

2.1 url 模块下 parse 函数

1、parse(获取地址信息)
const url = require('url');
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((request, response) => {
if (request.url != '/favicon.ico') {
console.log(url.parse('http://www.baidu.com?name=liu'));
};
response.statusCode = 200;
response.setHeader('Content-Type', 'text/plain;charset=utf-8');
response.end('url 模块下 parse 函数(传入参数)。');
});
server.listen(port, hostname, () => {
console.log(`服务器运行在 http://${hostname}:${port}`);
});

执行 node parse2.js,访问:127.0.0.1:3000/

Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.baidu.com',
port: null,
hostname: 'www.baidu.com',
hash: null,
search: '?name=liu',
query: 'name=liu',

pathname: '/',
path: '/?name=liu',
href: 'http://www.baidu.com/?name=liu' }
2、parse(parse 扩展)
const url = require('url');
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((request, response) => {
if (request.url != '/favicon.ico') {
/*
parse 方法可以传两个参数:
第一个参数是地址。
第二个参数是 true 的话表示把 get 传值转换成对象。
*/
const result = url.parse(request.url, true);
console.log(result);
console.log(result.query.userName);
console.log(result.query.userAge);
};
response.statusCode = 200;
response.setHeader('Content-Type', 'text/plain;charset=utf-8');
response.end('url 模块下 parse 函数(parse 扩展)。');
});
server.listen(port, hostname, () => {
console.log(`服务器运行在 http://${hostname}:${port}`);
});

  

 

执行 node parse3.js,访问:127.0.0.1:3000/?userName=liu&userAge=24

Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?userName=liu&userAge=24',
query: { userName: 'liu', userAge: '24' },
pathname: '/',
path: '/?userName=liu&userAge=24',
href: '/?userName=liu&userAge=24' }
liu
24

  

2.2 url 模块下 format 函数

format: 逆向 parse。

format.js

const url = require('url');
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((request, response) => {
   if (request.url != '/favicon.ico') {
       console.log(url.format({
           protocol: null,
           slashes: null,
           auth: null,
           host: null,
           port: null,
           hostname: null,
           hash: null,
           search: '?userName=liu&userAge=24',
           query: {
               userName: 'liu',
               userAge: '24'
           },
           pathname: '/',
           path: '/?userName=liu&userAge=24',
           href: '/?userName=liu&userAge=24'
       }));
   };
   response.statusCode = 200;
   response.setHeader('Content-Type', 'text/plain;charset=utf-8');
   response.end('url 模块下 format 函数。');
});
server.listen(port, hostname, () => {
   console.log(`服务器运行在 http://${hostname}:${port}`);
});

执行 node format.js,访问:127.0.0.1:3000/

/?userName=liu&userAge=24

2.3 url 模块下 resolve 函数

resolve: 追加或替换地址。

resolve.js

const url = require('url');
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((request, response) => {
   if (request.url != '/favicon.ico') {
       console.log(url.resolve('127.0.0.1:3000/?userName=liu&userAge=24', 'userName=zhao'));
   };
   response.statusCode = 200;
   response.setHeader('Content-Type', 'text/plain;charset=utf-8');
   response.end('url 模块下 resolve 函数。');
});
server.listen(port, hostname, () => {
   console.log(`服务器运行在 http://${hostname}:${port}`);
});

执行 node resolve.js,访问:127.0.0.1:3000/

127.0.0.1:3000/userName=zhao

node(http, url)的更多相关文章

  1. node.js url模块

    URL url.parse(urlStr[, parseQueryString][, slashesDenoteHost]) url.format(urlObj) url.resolve(from, ...

  2. 3.node的url属性

    node的url属性 1.parse: [Function: urlParse],2.format: [Function: urlFormat],3.resolve: [Function: urlRe ...

  3. 详解Node解析URL网址

    前提给大家声明一下,我操作的环境是Mac终端下操作的.(前提是你先要下载好node.js) 说道URL 恐怕都不陌生,但是要说URL,就 必须先说下URI URI是统一资源标识符,是一个用于标识某一互 ...

  4. Node fs, url, http 组合小型的服务器 ( 满足html请求, get, post 传值 )

    <script type="text/javascript"> /* * 引入模块 */ var http = require('http'); var url = r ...

  5. Node.js URL

    稳定性: 3 - 稳定 这个模块包含分析和解析 URL 的工具.调用 require('url') 来访问模块. 解析 URL 对象有以下内容,依赖于他们是否在 URL 字符串里存在.任何不在 URL ...

  6. Node.js——url模块

    url模块通过api可以将get提交的参数方便的提取出来

  7. node的url模块

    .parse(url,query2obj[boolean],ignorePrototype[boolean]) .format({}) 和.parse相反,将带有url参数属性的对象组装成url .r ...

  8. node获取URL数据

    req.method  -->GET req.hostname  -->127.0.0.1 req.originalUrl  -->/test/test/test?name=wang ...

  9. Node.js之HTPP URL

    几乎每门编程语言都会包括网络这块,Node.js也不例外.今天主要是熟悉下Node.js中HTTP服务.其实HTTP模块是相当低层次的,它不提供路由.cookie.缓存等,像Web开发中不会直接使用, ...

随机推荐

  1. RichText 富文本开源项目总结

    在Android开发中,我们不免会遇到富文本的编辑和展示的需求,以下是本人之前star的富文本编辑器的开源项目,供大家参考: 一.RichEditor 开源项目地址:https://github.co ...

  2. 给你的WordPress站点添加下雪特效

    今天看到这个教程,感觉挺应景的,就自己尝试了下,效果还行,没截GIF图 方法: 该js文件已支持https,同时已将其及相关雪花图片进行CDN加速处理,可直接调用. 找到WordPress主题的foo ...

  3. Android之.9图的知识

    Android之.9图的知识 .9图的介绍 .9图也称为pPatch图,它是android app开发里一种特殊的图片形式,文件的扩展名为:.9.png. 9patch图片的作用就是在图片拉伸的时候保 ...

  4. Android精通之Handler讲解

    版权声明:未经博主允许不得转载 一:简介 [达叔有道]软件技术人员,时代作者,从 Android 到全栈之路,我相信你也可以!阅读他的文章,会上瘾!You and me, we are family ...

  5. JS 实现触发下载内容(H5 download)

    概述 我对使用js控制下载非常感兴趣,在网上查资料的时候碰巧看到了相关实现方法,记录下来供以后开发时参考,相信对其他人也有用. 参考资料: JS前端创建html或json文件并浏览器导出下载 理解DO ...

  6. 《http权威指南》读书笔记14

    概述 最近对http很感兴趣,于是开始看<http权威指南>.别人都说这本书有点老了,而且内容太多.我个人觉得这本书写的太好了,非常长知识,让你知道关于http的很多概念,不仅告诉你怎么做 ...

  7. [Vuejs] svg-sprite-loader实现加载svg自定义组件

    1.安装 svg-sprite-loader npm install svg-sprite-loader -D 或者 npm install svg-sprite-loader --save-dev ...

  8. 《机器学习实战(基于scikit-learn和TensorFlow)》第六章内容学习心得

    本章讲决策树 决策树,一种多功能且强大的机器学习算法.它实现了分类和回归任务,甚至多输出任务. 决策树的组合就是随机森林. 本章的代码部分不做说明,具体请到我的GitHub上自行获取. 决策树的每个节 ...

  9. python常用库函数 - 备忘

    基础库 1. 正则表达式:re 符号 ()小括号 -- 分组 []中括号 -- 字符类,匹配所包含的任一字符 #注:字符集合把[]里面的内容当作普通字符!(-\^除外) {}大括号 -- 限定匹配次数 ...

  10. 在vue 中使用 less

    1.安装 npm install --save-dev less less-loader npm install --save-dev style-loader css-loader 先在index. ...