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开发中不会直接使用, ...
随机推荐
- Java项目启动时执行指定方法的几种方式
很多时候我们都会碰到需要在程序启动时去执行的方法,比如说去读取某个配置,预加载缓存,定时任务的初始化等.这里给出几种解决方案供大家参考. 1. 使用@PostConstruct注解 这个注解呢,可以在 ...
- phpspreadsheet导出数据到Excel
之前我们使用PHP导出Excel数据时使用的是PHPExcel库,但是phpoffice已经官方宣布PHPExcel已经被废弃不在维护,推荐使用phpspreadsheet,如下图所示 我们可以通过c ...
- MRO
在Python3里面,有多继承的时候,往往会出现调用Super失败的情况.Python里存在一种多继承 Super的调用顺序(C3算法),保证每个类调用一次. 体现:类名.__mro__ 使用Supe ...
- Android--UI之GridView
前言 这篇博客介绍一下Android平台下,GridView控件的开发.针对GridView控件的一些常用属性.方法,以及注意事项进行讲解,最后将以一个Demo展示GridView控件的使用. Gri ...
- 通过Calendar简单解析Date日期,获取年、月、日、星期的数值
有时候项目中需要用到Date的年.月.日.星期的数值.那么解析方法如下: /**解析日期,获取年月日星期*/ private void parseDateToYearMonthDayWeek(Date ...
- ①泡茶看数据结构-表ADT
前言 小朽,晚上回到寝室.烧了开水,又泡了一杯下午喝了的小毛尖.耳机听着萨克斯,总结下今天学的数据结构和算法中的表ADT. 表ADT节点: #单链表 #双链表 #循环链表 ...
- CSS从零开始(1)--CSS基础语法
1.CSS语法 CSS规则有两个主要部分构成:选择器,以及一条或多条说明. 例如:selector{declaration1;declaration2;declaration3;......;} 注: ...
- Unity3D热更新之LuaFramework篇[01]--从零开始
前言 因工作关系,需要对手头的项目进行热更新支持.了解后发现,Lua的几个变种:XLua.ToLua(原uLua)和Slua都可以做Unity热更,而ToLua更是提供了一个简易的热更框架--LuaF ...
- 解决 VS2017 打断点无效
打断点无效 断点显示白色,鼠标移上去,提示:The breakpoint will not currently be hit. No Symbols have been loaded for this ...
- php-fpm无法使用系统环境变量的解决方法
为了防止任意环境变量到达php-fpm进程,默认默认php-fpm是会清空系统环境变量的, 解决办法 修改php-fpm配置的clear_env = no (默认是yes)