02_Node js 基础模块(http,url)
栏目: JavaScript · 发布时间: 5年前
内容简介:http.js执行 node http.js,访问:127.0.0.1:3000/url.js
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(获取地址信息)
parse.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.parse('http://www.baidu.com')); }; 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 parse.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: null, query: null, pathname: '/', path: '/', href: 'http://www.baidu.com/' } 复制代码
2、parse(传入参数)
parse2.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.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' } 复制代码
3、parse(parse 扩展)
parse3.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') { /* 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 复制代码
以上所述就是小编给大家介绍的《02_Node js 基础模块(http,url)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 黑客基础,Metasploit模块简介,渗透攻击模块、攻击载荷模块
- TypeScript基础入门之模块(一)
- TypeScript基础入门之模块解析(一)
- TypeScript基础入门之模块解析(二)
- Swoole 源码分析——基础模块之 HashMap
- Java并发编程实战笔记3:基础构建模块
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Transcending CSS
Andy Clarke、Molly E. Holzschlag / New Riders / November 15, 2006 / $49.99
As the Web evolves to incorporate new standards and the latest browsers offer new possibilities for creative design, the art of creating Web sites is also changing. Few Web designers are experienced p......一起来看看 《Transcending CSS》 这本书的介绍吧!