node(http, url)
一、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)的更多相关文章
- node.js url模块
URL url.parse(urlStr[, parseQueryString][, slashesDenoteHost]) url.format(urlObj) url.resolve(from, ...
- 3.node的url属性
node的url属性 1.parse: [Function: urlParse],2.format: [Function: urlFormat],3.resolve: [Function: urlRe ...
- 详解Node解析URL网址
前提给大家声明一下,我操作的环境是Mac终端下操作的.(前提是你先要下载好node.js) 说道URL 恐怕都不陌生,但是要说URL,就 必须先说下URI URI是统一资源标识符,是一个用于标识某一互 ...
- Node fs, url, http 组合小型的服务器 ( 满足html请求, get, post 传值 )
<script type="text/javascript"> /* * 引入模块 */ var http = require('http'); var url = r ...
- Node.js URL
稳定性: 3 - 稳定 这个模块包含分析和解析 URL 的工具.调用 require('url') 来访问模块. 解析 URL 对象有以下内容,依赖于他们是否在 URL 字符串里存在.任何不在 URL ...
- Node.js——url模块
url模块通过api可以将get提交的参数方便的提取出来
- node的url模块
.parse(url,query2obj[boolean],ignorePrototype[boolean]) .format({}) 和.parse相反,将带有url参数属性的对象组装成url .r ...
- node获取URL数据
req.method -->GET req.hostname -->127.0.0.1 req.originalUrl -->/test/test/test?name=wang ...
- Node.js之HTPP URL
几乎每门编程语言都会包括网络这块,Node.js也不例外.今天主要是熟悉下Node.js中HTTP服务.其实HTTP模块是相当低层次的,它不提供路由.cookie.缓存等,像Web开发中不会直接使用, ...
随机推荐
- Open系列相关概念汇总
最近接触了Android OpenGL ES 和 OpenCL ES,然后就很想知道除了这两个之外到底还有几个Open系列的API集.搜集的结果如下(纯为自己科普): 1. OpenGL(OpenGr ...
- Java工程师必备
Java工程师必备 JAVA基础扎实,熟悉JVM,熟悉网络.多线程.分布式编程及性能调优 精通Java EE相关技术 熟练运用Spring/SpringBoot/MyBatis等基础框架 熟悉分布式系 ...
- SpringMVC框架四:异常处理器
.异常分为:预期异常.运行时异常 dao.service.controller三层中有异常,依次向上抛出直到SpringMVC处理. 而SpringMVC交给HandlerExceptionResol ...
- netty中的传输
终于在课设的闲时间把netty实战的四五章给解决了 这里来记录一下第四章里面所讲的IO 首先说到IO,我想,必须要先了解阻塞,非阻塞,同步和异步这四个词 看到一个讲的很易懂的例子:https://ww ...
- 使用TPC-DS工具生成数据
1.下载工具 两种渠道 a.官网(建议直接跳过-) 地址:http://www.tpc.org/tpc_documents_current_versions/current_specification ...
- centos 7 linux 安装与卸载 jdk 7
一.声明 本文采用操作系统版本: Centos 7 Linux 系统 版本源:CentOS-7-x86_64-DVD-1708.iso 官网下载地址:http://isoredirect.centos ...
- 【原创】VirtualBox 磁盘扩容教程
问题和环境说明 环境: 主机:Ubuntu 15.10 客户机:Windows 7 x64 VirtualBox:5.0.10 虚拟机磁盘类型:VDI(VirtualBox磁盘映像) 问题: 在虚拟机 ...
- 【原创】深入理解c++的右值引用
0 左值和右值 一个左值表达式代表的是对象本身,而右值表达式代表的是对象的值:变量也是左值. 1 右值引用作用 为了支持移动操作(包括移动构造函数和移动赋值函数),C++才引入了一种新的引 ...
- secureCRT常见命令
一.ls 只列出文件名 (相当于dir,dir也可以使用) -A:列出所有文件,包含隐藏文件. -l:列表形式,包含文件的绝大部分属性. -R:递归显示. --help:此命令的帮助. 二.cd 改变 ...
- Mybatis(六) Spring整合mybatis
心莫浮躁~踏踏实实走,一步一个脚印,就算不学习,玩,能干嘛呢?人生就是那样,要找点有意思,打发时间的事情来做,而钻研技术,动脑动手的过程,还是比其他工作更有意思些~ so,努力啥的都是强迫自己做自以为 ...