一、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. linux下如何把php升级到5.6

    1: 进入终端后查看php版本 php -v 输出可能如下: PHP (cli) (built: Nov ::) Copyright (c) - The PHP Group Zend Engine v ...

  2. java小白之面向对象

    面向对象 面相对象(oop)和面向过程(pop)通常一起说,一个是更加关注过程,事力亲为,而面向对象更加注重结果,所以说,面向对象更加是一种思想,它贯穿整个java,以上帝视角来看整个功能需求,简化开 ...

  3. 最大黑区域-DFS

    最大黑区域 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practic ...

  4. FileZilla 使用笔记

    FileZilla 使用了三年了,一些功能其实还没有主动去发现,这次接着项目忙完的空闲时间整理一下 Tips,提高工作效率,方便以后查阅. 一.好用的功能 1.Site Manager - 站点管理器 ...

  5. 比较empty()与 isset()d的区别

    比较empty()与 isset()的区别 注意:empty()在PHP5.5之前只能检测变量 isset()只能检测变量 两者之间的联系:empty($var) 等价于 !isset($var)|| ...

  6. Redis 常用操作命令,非常详细!

    下面总结并演示了 Redis 的 常用管理命令.key 操作.字符串.集合.列表.散列类型的操作命令. 你需要掌握的 Redis 知识 史上最全 Redis 高可用解决方案总结 为什么分布式一定要有R ...

  7. .NET Core 获取 HttpContext.Current 以及 AsyncLocal 与 ThreadLocal

    在 DotNetCore 当中不再像 MVC5 那样可以通过 HttpContext.Current 来获取到当前请求的上下文. 不过微软提供了一个 IHttpContextAccessor 来让我们 ...

  8. 技术简历这样写,才能得到BAT面试官的青睐

    公众号[程序员江湖] 作者陆小凤,985 软件硕士,阿里 Java 研发工程师,在技术校园招聘.自学编程.计算机考研等方面有丰富经验和独到见解,目前致力于分享程序员干货和学习经验,同时热衷于分享作为程 ...

  9. 2015 CALLED THE INTERFACE OF 2014

    Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. ”Hi , Happy New Year.Wr ...

  10. DRDS SQL 审计与分析——全面洞察 SQL 之利器

    背景 数据库存储着系统的核心数据,其安全方面的问题在传统环境中已经成为泄漏和被篡改的重要根源.而在云端,数据库所面临的威胁被进一步的放大.因此,对云数据库的操作行为尤其是全量 SQL 执行记录的审计日 ...