zoukankan      html  css  js  c++  java
  • nodejs复习05

    stream

    可读流

    • fs.pause()方法会使处于流动模式的流停止触发data事件,切换到非流动模式并让后续数据流在内部缓冲区
    var fs = require('fs')
    
    var rs = fs.createReadStream('2.txt');
    
    rs.setEncoding('utf8')
    
    //当一个数据快可以从流中被读出触发
    rs.on('readable', function () {
    	console.log('readable event');
    })
    
    //读取数据块时操作
    rs.on('data', function (chunk) {
    	console.log('data event');
    	rs.pause();  //停止
    	setTimeout(function () {
    		rs.resume() //恢复
    		console.log(chunk);
    	}, 3000)
    })
    
    //数据接收发生错误时
    rs.on('error', function (err) {
    	console.log('error event');
    })
    
    //没有更多数据能够提供时
    rs.on('end', function () {
    	console.log('end event');
    })
    
    //底层数据源(如数据源文件描述符)被关闭时触发
    //注意不是所有流都会触发
    rs.on('close', function () {
    	console.log('close event');
    })
    
    
    • 可读流有两种模式: 流动模式和暂停模式,暂停模式必须明确调用stream.read(size)方法,默认流动模式
    • read发明合法用语从内部缓存区中读取并返回数据;没有设置size则一次返回所有缓冲区数据
    • read方法仅在暂停模式时被调用,流动模式会自动调用
    //暂停模式,无需再设置data事件,readable会被调用两次
    rs.on('readable', function () {
    	console.log('readable event');
    	var chunk
    
    	while((chunk = rs.read()) !== null) {
    		console.log(chunk)
    	}
    })
    

    使用可读流发送数据

    • 可读流在接收者么哦呦读取数据之前会缓存所有压入的数据
    var stream = require('stream')
    var rs = new stream.Readable
    
    rs.push('Stream')  //发送数据
    rs.push('Readable')
    rs.push('Push()')
    rs.push('Pipe()')
    rs.push('
    ')
    rs.push(null)  //通知发送数据完毕
    rs.pipe(process.stdout)  //pipe方法导出数据
    

    pipe方法

    • 使用pipe时回自动调用data/end事件
    //复制文件
    var readable = fs.createReadStream('a.js');
    var writeable = fs.createWriteStream('c.js');
    readable.pipe(writeable)
    
    

    unpipe方法

    • 会解除之前调用pipe所设定的流;
    var readable = fs.createReadStream('a.js');
    var writeable = fs.createWriteStream('c.js');
    readable.pipe(writeable)
    
    //指定了目标c.js文件会变空白
    readable.unpipe(writeable);
    writeable.end()
    
    
    //指定了目标但没有建立导流, 不会影响
    setTimeout(function (){
    	readable.unpipe(writeable);
    	writeable.end()
    }, 100)
    

    可写流

    • ws.write方法向底层系统写入数据,并在数据被处理完毕后调用回调函数;如果数据滞留在内部则返回false
    var fs = require('fs')
    
    var ws = fs.createWriteStream('1.txt');
    ws.write('stream-');
    ws.write('writable-');
    ws.write('file-');
    ws.end('end
    ')
    

    两者共同使用

    var fs = require('fs')
    
    var rs = fs.createReadStream('a.js');
    var ws = fs.createWriteStream('1.txt');
    
    rs.setEncoding('utf8')
    
    rs.on('data', function (chunk) {
    	if(ws.write(chunk) === false) rs.pause()
    })
    
    ws.on('drain', function () {
    	rs.resume()
    })
    
    //ws.end被调用且所有数据已经被写入底层系统
    ws.on('finish', function() {
    	console.log('ws finish');
    })
    
    rs.on('end', function () {
    	ws.end()
    })
    

    http

    http服务器

    var http = require('http')
    var url = require('url')
    
    http.createServer(function (req, res) {
            var pathname = url.parse(req.url).pathname
            if(pathname === '/') {
                res.writeHead(200, {'Content-Type': 'text/html'})
                res.write('<h3>Nodejs-Http</h3>')
    	    res.end('<p>Create Basic HTTP Server!</p>')
            } else {
                res.writeHead(301, {'Location':'/'})
                res.end()
            }
    }).listen(3000)
    
    
    • setHeader/getHeader: setHeader方法用于读取一个在队列中但没有被发送至客户端的header
    • writeHeade方法只能在当前请求中使用一次,必须在write/end之前调用

    http客户端

    var http = require('http')
    
    var options = {
    	hostname: '127.0.0.1',
    	port: 3000,
    	path: '/',
    	method: 'get'
    }
    
    
    var req = http.request(options, function (res) {
    	console.log('STATUS: ' + res.statusCode);
    	console.log('HEADERS: ' + JSON.stringify(res.headers));
    	res.setEncoding('utf8')
    	var body = ''
    	res.on('data', function (chunk) {
    		body += chunk
    	})
    	res.on('end', function () {
    		console.log('BODY: ' + body);
    	})
    })
    
    req.on('error', function (err) {
    	console.log('err: ' + err);
    })
    
    req.write('data
    ')
    
    req.end()
    
    • 简化get调用: http.get(url, callback)

    util

    格式化字符串

    util.format(format. args)
    
    %s
    %d
    %j
    
  • 相关阅读:
    mysql 格林威治时间
    设置Highcharts刻度间距
    在PHP中判断单个字符是不是中文字符
    将ip以整形数据存储在数据库中的相关操作
    【Demo 0016】Windows 基本数据结构
    【Demo 0019】编码转换
    【Demo 0018】文字编码
    【Demo 0014】图形对象路径
    【Demo 0010】绘图对象区域
    【Demo 0017】Win32 基本结构体(1)
  • 原文地址:https://www.cnblogs.com/jinkspeng/p/6083538.html
Copyright © 2011-2022 走看看