Learn Node.js from scratch

Learn Node.js from scratch

url module

1.parse method

//test02.js
import http from 'http'
import url from 'url'
const parseUrl = url.parse('https://www.baidu.com/news?name=朱葛亮&age=18#helloworld')
console.log(parseUrl)
http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'})
    res.write('Hello, hello world!')
    res.end()
}).listen(3000)
console.log('My server is running at http://localhost:3000')

Parse the URL address and obtain a parsed URL details object, including protocol, domain name, path, port, query parameters, hash and other information.

The second parameter is a boolean value, which defaults to false. If true is passed, the query will be converted to an object.

const parseUrl = url.parse('https://www.baidu.com/news?name=朱葛亮&age=18#helloworld', true)
console.log(parseUrl)

2. format method

Pass in a URL information object (that is, the object returned by the parse method) and return a specific path. This method is the reverse application of the parse method.

const formatUrl = url.format({
    protocol: 'https:',
    slashes: true,
    auth: null,
    host: 'www.baidu.com',
    port: null,
    hostname: 'www.baidu.com',
    hash: '#helloworld',
    search: '?name=Zhuge Liang&age=18',
    query: 'name=Zhuge Liang&age=18',
    pathname: '/news',
    path: '/news?name=Zhuge Liang&age=18',
    href: 'https://www.baidu.com/news?name=朱葛亮&age=18#helloworld'
})
console.log(formatUrl) // Output https://www.baidu.com/news?name=朱葛亮&age=18#helloworld

3. resolve method

Splicing or replacing secondary paths

const result1 = url.resolve('https://www.baidu.com', 'news')
const result2 = url.resolve('https://www.baidu.com/home', '')
const result3 = url.resolve('https://www.baidu.com/home', 'about')
const result4 = url.resolve('https://www.baidu.com/home/index', 'about')
const result5 = url.resolve('https://www.baidu.com/home/index?name=朱葛亮', 'about/hello')
console.log(result1)
console.log(result2)
console.log(result3)
console.log(result4)
console.log(result5)

Output:

Events module (event-driven)

1. Introduce the event module

2. Create an eventEmitter instance

3. Use the on method and emit method in eventEmitter to implement event-driven, similar to $on and $emit in vue, that is, publish-subscribe mode

Asynchronous requirements can be solved as follows:

import fs from 'fs'
import event from 'events'

const eventEmitter = new event.EventEmitter()

eventEmitter.on('events', data => {
    console.log('Received data', data.toString())
})

fs.readFile('static/index.html', (err, data) => {
    eventEmitter.emit('events', data)
})

The path module

import path from 'path'
// Get the suffix name const extName = path.extname('index.html') // .html

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Learning Node.js from scratch series tutorial: Example of setting HTTP header method
  • Learn Node.js from scratch series of tutorials SQLite3 and MongoDB usage analysis
  • Learning Node.js from scratch series tutorial 6: Example of how EventEmitter sends and receives events
  • Learning Node.js from scratch series 5: Server monitoring method example
  • Learning Node.js from scratch series 4: Client-side and server-side examples of implementing mathematical operations on multiple pages
  • Learning Node.js from scratch series tutorial: Multi-page mathematical operation example based on connect and express framework
  • Learning Node.js from scratch series 4: Mathematical operation examples implemented on multiple pages

<<:  How to install the latest version of docker using deepin apt command

>>:  Implementation process of row_number in MySQL

Recommend

SQL implementation of LeetCode (181. Employees earn more than managers)

[LeetCode] 181.Employees Earning More Than Their ...

Implement group by based on MySQL to get the latest data of each group

Preface: The group by function retrieves the firs...

Summary of several replication methods for MySQL master-slave replication

Asynchronous replication MySQL replication is asy...

Several ways to update batches in MySQL

Typically, we use the following SQL statement to ...

Mysql SQL statement operation to add or modify primary key

Add table fields alter table table1 add transacto...

MySQL 8.0.12 decompression version installation tutorial

This article shares the installation tutorial of ...

Pure js to achieve the effect of carousel

This article shares the specific code of js to ac...

mysql5.7.19 winx64 decompressed version installation and configuration tutorial

Recorded the installation tutorial of mysql 5.7.1...

Common browser compatibility issues (summary)

Browser compatibility is nothing more than style ...

In-depth explanation of the principle of MySQL Innodb index

introduction Looking back four years ago, when I ...

VUE implements timeline playback component

This article example shares the specific code of ...

Use of environment variables in Docker and solutions to common problems

Preface Docker can configure environment variable...

Detailed explanation of the correct way to install opencv on ubuntu

This article describes how to install opencv with...

Detailed explanation of TypeScript 2.0 marked union types

Table of contents Constructing payment methods us...