一, 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(获取地址信息)
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
期待您的关注!
来源: https://juejin.im/post/5c676ba1e51d451b240a7aaa