node基础,快速入门node.js(2)

135 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第5天,点击查看活动详情

url模块

url模块提供用于 URL 解析和解析请求参数的方法。可以使用以下方式访问它:

const url = require("url");

url模块提供了两个用于处理 URL 的 API:一个是特定于 Node.js 的Legacy API,另一个是实现 Web 浏览器使用的相同WHATWG URL 标准的新 API。

Legacy API 旧版

const url = require("url");
const myURL =url.parse('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash', true); //第二个参数设置为true可以把参数解析成对象
console.log(myURL);
​
//返回的对象
Url {
  protocol: 'https:',
  slashes: true,
  auth: 'user:pass',
  host: 'sub.example.com:8080',
  port: '8080',
  hostname: 'sub.example.com',
  hash: '#hash',
  search: '?query=string',
  query: [Object: null prototype] { query: 'string' },
  pathname: '/p/a/t/h',
  path: '/p/a/t/h?query=string',
  href: 'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'
}

旧版API的大多数方法已经废弃,比较重要的有format() resolve()等方法,可以自行了解

WHATWG URL API 新版

const myURL = new URL('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash');
console.log(myURL);
​
//返回的对象
URL {
  href: 'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash',
  origin: 'https://sub.example.com:8080',
  protocol: 'https:',
  username: 'user',
  password: 'pass',
  host: 'sub.example.com:8080',
  hostname: 'sub.example.com',
  port: '8080',
  pathname: '/p/a/t/h',
  search: '?query=string',
  searchParams: URLSearchParams { 'query' => 'string' },
  hash: '#hash'
}

有了这个内置模块就能很好的解决之前前端带参数请求 list 时返回404的问题。可以看出两种API返回的对象的 pathname属性都是前端的请求地址,而且没有携带参数,现在只需要把传给函数的参数改成pathname就好了。

修改app,js

const http = require('http');
//导入module.js文件中导出的方法
const module = require('./modules/module.js');
//创建本地服务器来接收数据
const server = http.createServer((req, res) => {
    //解析请求地址
    const myurl = new URL(req.url, "http://localhost:3000");
    //如果请求地址是'/favicon.ico',直接返回
    if(req.url === '/favicon.ico') {
        return;
    }
    //修改传给函数的参数
    res.writeHead(module.readerStatus(myurl.pathname), { "Content-Type": "text/html;charset=utf-8" });
    res.write(module.readerHTML(myurl.pathname));
​
    res.end();
}).listen(3000, ()=>{
    console.log("http://localhost:3000");
});

启动服务 node app.js

这下就可以正常携带参数访问不同地址返回不同页面的信息了。

querystring模块

querystringAPI 被视为旧版。虽然它仍在维护,但新代码应该使用URLSearchParams API。

querystring模块提供了用于解析和格式化 URL 参数字符串的方法。可以使用以下方式访问它:

const querystring = require("querystring");

parse()

let str = 'name=john&age=20&address=chengdu'
let obj = querystring.parse(str)
console.log(obj);
//返回对象
[Object: null prototype] {
  name: 'john',
  age: '20',
  address: 'chengdu'      
}

stringify()

let myobj = {
    a: 1,
    b: 2,
    c: 3
}
let mystr = querystring.stringify(myobj);
console.log(mystr);
//返回的字符串
a=1&b=2&c=3

escape() 和 unescape()

let str1 = 'id=1&name=john&age=20&address=成都'
let escape = querystring.escape(str1);
console.log(escape); //id%3D1%26name%3Djohn%26age%3D20%26address%3D%E6%88%90%E9%83%BDlet unescape = querystring.unescape('id%3D1%26name%3Djohn%26age%3D20%26address%3D%E6%88%90%E9%83%BD');
console.log(unescape); //id=1&name=john&age=20&address=成都