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)

以上所述就是小编给大家介绍的《02_Node js 基础模块(http,url)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web性能优化

Web性能优化

Patrick Killelea 谢 / 谢文亮 / 清华大学出版社 / 2003-11-01 / 49.00元

本书讲述如何将Web性能调至最佳状态。书中不仅谈到了Web服务器软件的优化,而且还涉及到如何流水化处理Web内容,如何从浏览器端着手优化性能,如何调校客户端和服务器的硬件,以及如何最大限度地使用网络本身的特性。 书中的内容涉及到影响性能好坏的本质,并为得到立竿见影的效果提供了具体建议。本书向您娓娓道出评价计算性能高低的准则,并在后半部分讲述从客户端、网络直到服务器这一链条中每个环节的薄弱之一起来看看 《Web性能优化》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具