nodejs - The next day, nodeAPI-URL/querystring/HTTP/events/fs

Keywords: PHP JSON Attribute Javascript

1. URL

1. 1. url.parse

url.parse(urlString[, parseQueryString[, slashesDenoteHost]])

url.parse('https://www.baidu.com:8080/api.php?from=zj&age=12#level1')
Url {
  protocol: 'https:', // Agreement
  slashes: true, // 
  auth: null,
  host: 'www.baidu.com', // Host
  port: null, // port
  hostname: 'www.baidu.com', // Domain Name/Host Name
  hash: 'level1', 
  search: '?from=zj&age=12', // parameter
  query: 'from=zj&age=12', // Content of parameter analysis
  pathname: '/api.php', // Path name
  path: '/api.php?from=zj&age=12', // Route
  href: 'https://www.baidu.com:8080/api.php?from=zj&age=12#level1' // Complete url address returned
  }

When the second parameter is true, the query content is the object
When the protocol is not known, the third parameter is set to true, which can parse all the parameters except the protocol, and can not parse host, port and hostname without passing. Here's a comparison

> url.parse('//www.baidu.com:8080/api.php?from=zj&age=12#level1',true,true)
Url {
  protocol: null,
  slashes: true,
  auth: null,
  host: 'www.baidu.com:8080',
  port: '8080',
  hostname: 'www.baidu.com',
  hash: '#level1',
  search: '?from=zj&age=12',
  query: { from: 'zj', age: '12' },
  pathname: '/api.php',
  path: '/api.php?from=zj&age=12',
  href: '//www.baidu.com:8080/api.php?from=zj&age=12#level1' }
> url.parse('//www.baidu.com:8080/api.php?from=zj&age=12#level1',true)
Url {
  protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: '#level1',
  search: '?from=zj&age=12',
  query: { from: 'zj', age: '12' },
  pathname: '//www.baidu.com:8080/api.php',
  path: '//www.baidu.com:8080/api.php?from=zj&age=12',
  href: '//www.baidu.com:8080/api.php?from=zj&age=12#level1' }
query: { from: 'zj', age: '12' } 

1. 2. url.format parses the parameters obtained by url.parse into URL addresses

url.format(urlObject)

1. 3. url.resolve two-segment url resolves to url

 url.resolve('https://www.baidu.com','/api.php')
'https://www.baidu.com/api.php'

2. querystring (query string)

Analytical parameter module
Object -> URL
url -> object
Codable and decodable

2. 1.querystring.stringify(obj[, sep[, eq[, options]]), which generates a URL query string from a given obj

  1. The first parameter obj is serialized as an object of a URL query string.
> querystring.stringify({name:'zj',course:['nodejs','js']})
'name=zj&course=nodejs&course=js'
  1. The second parameter: sep is used to define the substring of the key-value pair in the query string. The default is'&'.
> querystring.stringify({name:'zj',course:['nodejs','js']},',')
'name=zj,course=nodejs,course=js'
  1. The third parameter: eq is a substring that defines the keys and values in the query string. The default is'='.
> querystring.stringify({name:'zj',course:['nodejs','js']},',',':')
'name:zj,course:nodejs,course:js'

2. 2. querystring.parse(str[, sep[, eq[, options]]]) parses a URL query string (str) into a set of key-value pairs.

  1. obj is serialized as an object of a URL query string.
  2. sep is used to define the substrings of key-value pairs in query strings. The default is'&'.
  3. eq is a substring used to define keys and values in a query string. The default is'='.
  4. options
    encodeURIComponent is a function used when converting characters that are insecure to URL s into percentile codes in query strings. The default is querystring.escape().
querystring.parse('name:zj,course:nodejs,course:js')
{ 'name:zj,course:nodejs,course:js': '' }

2.3. querystring. escape (str) coding

> querystring.escape('text')
'%E6%96%87%E6%9C%AC'

2.4. querystring. unescape (str) decoding

> querystring.unescape('text')
'text'

3. HTTP

3.1 https.get() reptile

https.get(options, callback)

var https = require('https');
var cheerio = require('cheerio');
var url = 'https://www.lagou.com/';

https.get(url, function(res){
    var html = '';
    res.on('data', function(data){
        html +=data;
    });

    res.on('end', function(){
        var result = filterMenu(html);
        printMenu(result);
    });

    res.on('error', function(err){
        console.log(err);
    });
});

function filterMenu(html){
    var $ = cheerio.load(html);
    var menu = $('.menu_main');
    var menuData = [];
    menu.each(function(index, value){
        var menuTitle = $(value).find('h2').text();
        var menuLists = $(value).find('a');
        var menuList = [];
        menuLists.each(function(index, value){
            menuList.push($(value).text());
        });
        menuData.push({
            menuTitle: menuTitle,
            menuList: menuList
        });
    });
    return menuData;
}

function printMenu(menu){
    menu.forEach(function(value) {
        console.log(value.menuTitle + '\n');
        value.menuList.forEach(function(value){
            console.log(value);
        });
    });
}

3.2 https.request GET Gets Douban api Movie List

https.request(options, callback) initiates a request to a secure server.
https.request() callback function
Respons.statusCode returns status code
response.headers return the request header
Douban api movie list address:
https://api.douban.com/v2/movie/top250

var https = require('https');

var options = {
    hostname: 'api.douban.com',
    port: 443,
    method: 'GET',
    path: '/v2/movie/top250'
};

var responseData = '';
var request = https.request(options, (response)=>{
    // console.log(response.statusCode);
    // console.log(response.headers);

    response.setEncoding('utf-8');
    response.on('data', (chunk)=>{
        responseData += chunk;
    });
    response.on('end', ()=>{
        JSON.parse(responseData).subjects.map((item) => {
             console.log(item);
        });
    });

});
request.on('error', (err)=>{
    console.log(error);
});

request.end();

3. 3 http.request POST response comments

var http = require('http');
var querystring = require('querystring');

var postData = querystring.stringify({
    'question[title]':'The video is good.',
    'question[content]':'<p>Fabulous</p>',
    'question[courseId]':'227',
    'question[lessonId]':'1753',
    '_csrf_token':'9f31b020868a378fb7b1770c2ada7e2eb46011b8'
});

var options = {
    hostname : 'www.codingke.com',
    port: 80,
    method: 'POST',
    path: '/ajax/create/course/question',
    headers:{
        'Accept':'*/*',
        'Accept-Encoding':'gzip, deflate',
        'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6',
        'Connection':'keep-alive',
        'Content-Length': postData.length,
        'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie':'UM_distinctid=15c06f3177aae9-041a43a2cea6-30647509-1fa400-15c06f3177bcf6; PHPSESSID=s6ta4rvvp1v5gavt58ulcvmbr2; CNZZDATA1256018185=1474220391-1494760065-null%7C1494767532; Hm_lvt_9f92046de4640f3c08cf26535ffdd93c=1494765214; Hm_lpvt_9f92046de4640f3c08cf26535ffdd93c=1494772698',
        'Host':'www.codingke.com',
        'Origin':'http://www.codingke.com',
        'Referer':'http://www.codingke.com/v/355-chapter-222-course',
        'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
        'X-CSRF-Token':'9f31b020868a378fb7b1770c2ada7e2eb46011b8',
        'X-Requested-With':'XMLHttpRequest'
    }
};


var request = http.request(options, (response)=>{
   console.log('statue:' + response.statuesCode);
   console.log('sheaders:' + JSON.stringify(response.headers));
   response.setEncoding('utf8');
   response.on('data', (chunk)=>{
    console.log(chunk);
   });
   response.on('end', ()=>{
       console.log('Technical Questions and Answers Completed');
   });
});
request.on('error', (error) => {
    console.log(error);
});

request.write(postData);

request.end();

4. Evets events

A net.Server object triggers an event every time a new connection is made.
An fs.ReadStream triggers an event when the file is opened.
A stream triggers events when the data is readable.
All objects that trigger events are instances of the EventEmitter class.
These objects open an eventEmitter.on() function that allows attachment of one or more functions to named events triggered by the object.
The eventEmitter.on() method is used to register listeners, and the eventEmitter.emit() method is used to trigger events.
The following example is an EventEmitter instance with only a single listener

const EventEmitter = require('events');

class Player extends EventEmitter {}

var player = new Player()

player.on('play', (strack)=>{
    console.log(`Playing now<${strack}>`)
})

player.emit('play', 'hello')
player.emit('play', 'world')
//Printing
//Hello is playing
//Playing < world >

EvetEmitter. once () handles events only once

player.on('play', (strack)=>{
    console.log(`Playing now<${strack}>`)
})

player.emit('play', 'hello')
player.emit('play', 'world')
//Printing
//Playing <hello>

5. File System File System

  • Get information about files and directories: stat
  • Create a directory: mkdir
  • Create files and write content: writeFile, appendFile
  • Read the contents of the file: readFile
  • What lists directories: readdir
  • Rename directory or file: rename
  • Delete directories and files: rmdir, unlink

5.1 fs.stat gets file information.

const fs = require('fs')

fs.stat('event.js', (error, stats)=>{
    if(error){
        console.log(error)
    } else {
        console.log(stats)
        console.log(`Document: ${stats.isFile()}`)
        console.log(`Document: ${stats.isDirectory()}`)
    }
})
  • Is stats.isFile() a file?
  • Is stats.isDirectory() a directory?
    Printing
{ dev: 16777220,
  mode: 33188,
  nlink: 1,
  uid: 501,
  gid: 20,
  rdev: 0,
  blksize: 4096,
  ino: 6188115,
  size: 242,
  blocks: 8,
  atime: 2017-05-14T15:05:53.000Z,
  mtime: 2017-05-14T15:05:52.000Z,
  ctime: 2017-05-14T15:05:52.000Z,
  birthtime: 2017-05-14T14:50:15.000Z }
//Document:true
//Document:false

stat time value

  • atime access time
  • mtime modification time
  • ctime change time
  • birthtime creation time
    They are all instances of Data objects.
    stats.birthtime.getTime() Gets the number of milliseconds

5.2 fs.mkdir Create Directory

const fs = require('fs')

fs.mkdir('logs', (err)=>{
    if(err){
        console.log(err)    
    } else {
        console.log('Successfully created logs Catalog')
    }
})

5.3 fs.writeFile writes to file

If the file does not exist, it will be created

5.4 fs.appendFile additions

const fs = require('fs')

fs.writeFile('logs/hello.log', 'hello ~ \n', (err)=>{
    if(err){
        console.log(err)    
    } else {
        console.log('Successful write to file')
    }
})

fs.appendFile('logs/hello.log', 'Hello ~\n', (err)=>{
    if(err) {
        console.log(err)
    } else {
        console.log('Successful addition of content')
    }
})

5.5 fs.readFile reads files

const fs = require('fs')

fs.readFile('logs/hello.log', (err, data)=>{
    if(err){
        console.log(err)    
    } else {
        console.log(data.toString())
        console.log('Successful write to file')
    }
})

The default is the buffer type. You can use the toString method, or pass the encoding type'utf8'to the fs.readFile method, or get the original content.

fs.readFile('logs/hello.log','utf8', (err, data)=>{
    if(err){
        console.log(err)    
    } else {
        console.log(data.toString())
        console.log('Successful write to file')
    }
})

5.6 fs.readdir reads the contents of a directory

const fs = require('fs')

fs.readdir('logs', (err, files)=>{
    if(err){
        console.log(err)    
    } else {
        console.log(files)
    }
})
//Printing
//[ 'hello.log' ]

5.7 fs.rename rename

const fs = require('fs')

fs.rename('logs/hello.log','logs/greeting.log', (err)=>{
    if(err){
        console.log(err)    
    } else {
        console.log('Successful renaming')
    }
})

5.8 fs.rmdir Delete Directories

const fs = require('fs')
fs.rmdir('logs', (err)=>{
    if(err){
        console.log(err)    
    } else {
        console.log('Delete successful')
    }
})
//The console reported an error.logsThere are files in the directory and they cannot be deleted. 

You need to delete the file first and add some code.

fs.readdirSync('logs').map((file)=>{
    fs.unlink(`logs/${file}`, (err)=>{
        if (err) {
            console.log(err)
        } else {
            console.log(`The file was deleted successfully: ${file}`)
        }
    })
})

6. Stream stream

  • Read File Stream
  • Events of Readable Stream
  • Writable file stream
  • pipe
  • Chain using pipe

6.1 fs.createReadStream('data.json') reads the file stream

const fs = require('fs')

var fileReadStream = fs.createReadStream('data.json')

var count = 0;

fileReadStream.once('data', (chunk)=>{
    console.log(chunk)
})
fileReadStream.on('data', (chunk)=>{
    console.log(`${++count}Received: ${chunk.length}`)
})
fileReadStream.on('end', ()=>{
    console.log('End···')
})
fileReadStream.on('err', (err)=>{
    console.log(err)
})

data.json is the top 100 data of Douban Movie Top250: https://api.douban.com/v2/movie/top250?count=100
Terminal output:

<Buffer 7b 0a 20 20 20 20 22 63 6f 75 6e 74 22 3a 20 31 30 30 2c 0a 20 20 20 20 22 73 74 61 72 74 22 3a 20 30 2c 0a 20 20 20 20 22 74 6f 74 61 6c 22 3a 20 32
... >
1 Received: 65536
 2 Received: 65536
 3 Received: 65536
 4 Received: 65536

6.2 fs.createWriteStream('data.json') writes to the file stream

var fileWriteStream = fs.createWriteStream('data-1.json')
fileReadStream.on('data', (chunk)=>{
    console.log(`${++count}Received: ${chunk.length}`)
    fileWriteStream.write(chunk)
})

6.3 pipe

const fs = require('fs')

var fileReadStream = fs.createReadStream('data.json')
var fileWriteStream = fs.createWriteStream('data-1.json')

fileReadStream.pipe(fileWriteStream)

zlib compression

Copy and compress data.json through pipe pipeline

const fs = require('fs')
const zlib = require('zlib')

var fileReadStream = fs.createReadStream('data.json')
var fileWriteStream = fs.createWriteStream('data.json.gz')

fileWriteStream.on('pipe', (source)=>{
    console.log(source)
})

fileReadStream
.pipe(zlib.createGzip())
.pipe(fileWriteStream)

Posted by melrse on Sat, 15 Dec 2018 01:48:04 -0800