nodejs复习01
console
格式化
console.log("%s:%s", "a", "b") //字符串
console.log("%d.%d", 10.2, 0.12) //整型
console.log("%j", {a: "aa", b: "bb"}) //json
冲定向错误输出柳
- 对于
throw Erro, console.error, console.warn
node exec.js 2 > error.log
查看对象属性和方法
console.dir(obj)
//等价
var util = require('util')
console.log(util.inspect(obj));
计时器
console.time(label) //开始
console.timeEnd(label) //结束
查看当前调用栈
//打印当前位置的栈,并跟踪到标准错误输出流
console.trace(label)
断言
console.assert(expression, error message)
//相当于
var assert = require('assert')
assert.okt(expression, error message)
readline
命令行输入数据
var readline = require('readline')
var read = readline.createInterface({
input: process.stdin,
output: process.stdout
})
read.question("you name?", function(answer) {
console.log(answer);
read.close()
})
向控制台输出组合控制键
//3s后模拟ctrl+u
var readline = require('readline')
var read = readline.createInterface({
input: process.stdin,
output: process.stdout
})
read.write("Delete me! Wait for 3 seconds...")
var timer = setTimeout(function () {
read.write(null, {ctrl: true, name: 'u'})
}, 3000)
//read,write(data, key) key是一个代表键序列的对象
模拟控制台界面
//read.prompt(boolean) 为true或空时可以阻止命令提示符的光标被重置为0
var readline = require('readline')
var read = readline.createInterface({
input: process.stdin,
output: process.stdout
})
read.setPrompt('NodeJs>')
read.prompt()
read.on("line", function (line) {
switch(line.trim()) {
case 'book1':
console.log(100);
break;
case 'book2':
console.log(200);
break;
default:
console.log('no');
break;
}
read.prompt()
})
.on('close', function () {
console.log('bye');
process.exit(0);
})
module
node_modules文件加载
module.paths
: 数组返回加载的依次路径
module.exports 对象和exports对象
module.parent 指向该module被require时的module,可以用于测试判读
module.exports
是module
模块的真正接口,exports
是指向它的变量;- 所以设置
module.exports
后exports
设置就失效了
Buffer
js语言本身仅仅支持Unicode字符串数据处理
初始化
var buffer1 = new Buffer([0x6e, 0x6f, 0x64, 0x65, 0x6a, 0x73])
var buffer2 = new Buffer('nodejs')
//显示都为<Buffer 6e 6f 64 65 6a 73>, 16进制, hex
buffer.toString(encoding, start, end) // 默认utf8, 还有hex, binary, base64, utf16le, ascii
//特别定义
var buffer = new Buffer(length) /分配大小为length的8位字节
buffer.write(string, offset, length, encoding)
//判断是否为指定编码
buffer.isEncoding(encoding)
Buffer字节长度
str.length //字符长度
Buffer.byteLength(str, encoding) //字节长度
- 在改写
http
响应头Content-Length
时一定要使用该方法而不是修改length
基本操作
var buf = new Buffer('nodejs')
//裁减, buf.slice(start, end)
var buf1 = buf.slice() // 副本, buf1和buf2指向同一地址
buf1[0] = 97 //ASCII 'a'
console.log(buf.toString()); //anodejs
console.log(buf1.toString()); //anodejs
//拷贝 buf.copy(target, targetStart, sourceStart, sourceEnd)
var buf2 = new Buffer(buf.length)
buf.copy(buf2,0,0,buf.length)
buf2[0] = 110 //ASCII 'n'
console.log(buf.toString()); //anodejs
console.log(buf2.toString()); //nnodejs
//拼接 Buffer.concat(list, totalLength)
//如果totalLength没有提供会增加额外的计算
var list = [], len = 0;
for (var i = 0; i < 4; i++) {
list.push(buf)
len += buf.length
}
var buf3 = Buffer.concat(list, len)
console.log(buf3.toString());
nodejs复习01的更多相关文章
- Bone Collector(复习01背包)
传送门 题目大意:01背包裸题. 复习01背包: 题目 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总 ...
- nodejs复习02
process 这个模块是单线程的,无法完全利用多核CPU 基本信息 //程序目录 process.cwd(); //应用程序当前目录 process.chdir('/home'); //改变应用程序 ...
- codevs 2837 考前复习——01背包
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description Aiden马上要考试了,可他还没怎么复习,于是他 ...
- mybatis复习01
1.mybatis的历史: mybatis是apache的一个开源项目,2010被google收购,转移到google code. mybatis是一个优秀的持久层框架,对jdbc操作进行了封装,是操 ...
- nodejs复习05
stream 可读流 fs.pause()方法会使处于流动模式的流停止触发data事件,切换到非流动模式并让后续数据流在内部缓冲区 var fs = require('fs') var rs = fs ...
- nodejs复习04
TCP/UDP网络应用 创建TCP服务器客户端 socket套接字对象实例,是对TCP协议的一个基本封装接口 clientt套接字对象实例 //server.js var net = require( ...
- nodejs复习03
文件系统fs 重命名 fs.rename() fs.renameSync 优先选择异步,可以进行异常判断 打开关闭文件 fd = fs.openSync(file, flags) fs.closeSy ...
- C#基础总复习01
马上就快毕业了,准备把这几个月所学到的知识梳理一下,这儿所写的都是一些C#中最基础的东西(大牛不要笑话我,这也是我记录的一些笔记等等),希望能帮到一些正在学习这方面的知识的人,如果有写的不对的地方,望 ...
- nodejs弯路-01之'express' 不是内部或外部命令
最近正想用node+angular+mongodb来完成一个小项目,三样都算是从零开始学习吧. 一开始是想用express -e projectname去创建一个ejs模板的项目.(一两句话就可以把大 ...
随机推荐
- PL/SQL存储过程编程
PL/SQL存储过程编程 /**author huangchaobiao *Email:huangchaobiao111@163.com */ PL/SQL存储过程编程(上) 1. Oracle应用编 ...
- UvaLA 3938 "Ray, Pass me the dishes!"
"Ray, Pass me the dishes!" Time Limit: 3000MS Memory Limit: Unkn ...
- Block formatting context(块级格式化上下文)
今天看到豆瓣面试官的一篇文章,讲到关于CSS中的一个知识点:Block formatting context ,感觉这个确实挺有用,同时我也挺赞同作者的一些观点的,这里就不展开谈我的感受了, 此文只 ...
- PV、EV、AC、BAC、EAC、ETC等计算公式含义
PV.EV.AC.BAC.EAC.ETC等计算公式含义 PV Planned Value:计划值 应该完成多少工作, (按照计划截止目前应该花费的预算) AC Actual Cost:实际成本, 完成 ...
- 11.11光棍节工作心得——github/MVP
11.11光棍节工作心得 1.根据scrum meeting thirdday中前辈的指导进行学习 我在博客中贴了链接,竟然TrackBack引来了原博主,
- ES6学习笔记一
块级作用域:在ES5中只有全局作用域与函数作用域,ES6中新增的块级作用域避免变量的覆盖与泄露.考虑到代码运行环境,在块级作用域中应避免声明函数,最好用函数表达式的方式声明. let与const:le ...
- jquery复选框 选中事件 及其判断是否被选中
jquery复选框 选中事件 及其判断是否被选中 (2014-07-25 14:03:54) 转载▼ 标签: jquery复选框选中事件 分类: extjs jquery 今天做了 显示和不显示密 ...
- BZOJ3522——[Poi2014]Hotel
1.题意:http://www.lydsy.com/JudgeOnline/problem.php?id=3522 2.分析:这道题有两种情况,第一个是三个点在同一个链上,这显然不可能了,因为树上的路 ...
- Spark基础知识汇总
2,wordcount: val wordcount = sc.textFile()).reduceByKey(_ + _).sortByKey().collect val wordcount = s ...
- Python之mmap内存映射模块(大文本处理)说明
背景: 通常在UNIX下面处理文本文件的方法是sed.awk等shell命令,对于处理大文件受CPU,IO等因素影响,对服务器也有一定的压力.关于sed的说明可以看了解sed的工作原理,本文将介绍通过 ...