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开发中不会直接使用, ...
随机推荐
- centos7使用wordpress布署网站(2)
1.接下来需要配置数据库,为使用wordpress做准备 修改认证方式: vim .../phpMyAdmin/config.inc.php [...] $cfg['Servers'][$i]['au ...
- 第41节:Java当中的类集框架
Java当中的类集框架 类集框架,那么什么是类集框架,集合的种类有哪些,类集框架的基础结构. 类集框架是一组类和接口的集合,位于java.util包当中,是用来用户存储和管理对象的,在这个类集合框架中 ...
- FF中flash滚轮失效的解决方案
概述 在FF浏览器中有这样一个bug,就是当鼠标hover在flash区域的时候,滚轮会失效.原因是ff浏览器没有把滚轮事件嵌入到flash里面去.如果这个flash很小的话,比如直播的视频,会很容易 ...
- 如何编写JQuery 插件详解
转载自:http://blog.sina.com.cn/s/blog_6154bf970101jam7.html 如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jq ...
- C语言中指针中的值赋值给数组
如果把各种语言做个冷兵器类比的话,C语言一定是刀客的最佳工具.入门很简单,但是要是能把它熟练运用,那就是顶尖级别的高手了. 用了那么多年的C语言,发现自己还是仅仅处于熟练的操作工.今天遇到了一个bug ...
- ubuntu下makeinfo安装,其实真正安装的是texinfo包
操作系统环境:ubuntu 在终端中执行命令:sudo apt-get install texinfo 今天在打包的时候有个包需要 makeinfo,当时就各种搜结果就没有 makeinfo 这个 ...
- Spring lazy-init 原理分析
普通的bean的初始化是在容器启动初始化阶段执行的,而被lazy-init修饰的bean 则是在从容器里第一次进行context.getBean(“”)时进行触发.Spring 启动的时候会把所有be ...
- tensorflow 1.0 学习:用CNN进行图像分类
tensorflow升级到1.0之后,增加了一些高级模块: 如tf.layers, tf.metrics, 和tf.losses,使得代码稍微有些简化. 任务:花卉分类 版本:tensorflow 1 ...
- 有关 Azure 机器学习的 Net# 神经网络规范语言的指南
Net# 是由 Microsoft 开发的一种用于定义神经网络体系结构的语言. 使用 Net# 定义神经网络的结构使定义复杂结构(如深层神经网络或任意维度的卷积)变得可能,这些复杂结构被认为可提高对数 ...
- Android--解析XML之PULL
前言 在上一篇博客已经介绍了Android解析XML的几种方式,分别有:SAX.DOM.PULL.详细的可以看看上一篇博客:http://www.cnblogs.com/plokmju/p/andro ...