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的更多相关文章

  1. Bone Collector(复习01背包)

    传送门 题目大意:01背包裸题. 复习01背包: 题目 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总 ...

  2. nodejs复习02

    process 这个模块是单线程的,无法完全利用多核CPU 基本信息 //程序目录 process.cwd(); //应用程序当前目录 process.chdir('/home'); //改变应用程序 ...

  3. codevs 2837 考前复习——01背包

     时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description Aiden马上要考试了,可他还没怎么复习,于是他 ...

  4. mybatis复习01

    1.mybatis的历史: mybatis是apache的一个开源项目,2010被google收购,转移到google code. mybatis是一个优秀的持久层框架,对jdbc操作进行了封装,是操 ...

  5. nodejs复习05

    stream 可读流 fs.pause()方法会使处于流动模式的流停止触发data事件,切换到非流动模式并让后续数据流在内部缓冲区 var fs = require('fs') var rs = fs ...

  6. nodejs复习04

    TCP/UDP网络应用 创建TCP服务器客户端 socket套接字对象实例,是对TCP协议的一个基本封装接口 clientt套接字对象实例 //server.js var net = require( ...

  7. nodejs复习03

    文件系统fs 重命名 fs.rename() fs.renameSync 优先选择异步,可以进行异常判断 打开关闭文件 fd = fs.openSync(file, flags) fs.closeSy ...

  8. C#基础总复习01

    马上就快毕业了,准备把这几个月所学到的知识梳理一下,这儿所写的都是一些C#中最基础的东西(大牛不要笑话我,这也是我记录的一些笔记等等),希望能帮到一些正在学习这方面的知识的人,如果有写的不对的地方,望 ...

  9. nodejs弯路-01之'express' 不是内部或外部命令

    最近正想用node+angular+mongodb来完成一个小项目,三样都算是从零开始学习吧. 一开始是想用express -e projectname去创建一个ejs模板的项目.(一两句话就可以把大 ...

随机推荐

  1. JavaScript 构造函数与原型链

    构造函数.原型链: function Person(name, age, job) { this.name = name; this.age = age; this.job = job; // thi ...

  2. 动态令牌-(OTP,HOTP,TOTP)-基本原理

    名词解释和基本介绍 OTP 是 One-Time Password的简写,表示一次性密码. HOTP 是HMAC-based One-Time Password的简写,表示基于HMAC算法加密的一次性 ...

  3. 30多条mysql数据库优化方法,千万级数据库记录查询轻松解决(转载)

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  4. Python中两种处理错误方法的比较

    我所说的处理错误的方法,其实是try:,except和raise这两种. 首先抛出一个实例, dictt={'a':1,'b':2,'c':3} try: if dictt['d']>1: #字 ...

  5. 关于手机的内置SD卡与外置SD卡

    对于安卓2.3的系统来说,Environment.getExternalStorageDirectory()获取的目录是内置SD卡还是外置SD卡是无法保证的, 和手机厂商的修改有关,只能通过Envir ...

  6. 兼容8事件绑定与解绑addEventListener、removeEventListener和ie的attachEvent、detachEvent

    兼容8事件绑定与解绑addEventListener.removeEventListener和ie的attachEvent.detachEvent   ;(function(){ // 事件绑定 bi ...

  7. android3D动画,绕y轴旋转

    原文地址:http://blog.csdn.net/x_i_a_o_h_a_i/article/details/40449847 其实网上的3D旋转的例子很多,在这里我只是想把其代码做一个解释. 先上 ...

  8. composer 报错:Your requirements could not be resolved to an installable set of packages 解决方法

    composer 报错: - Your requirements could not be resolved to an installable set of packages xxxxxxxxxxx ...

  9. C#夯实基础之接口(《CLR via C#》读书笔记)

    一. 接口的类型 接口是引用类型.因此从值类型赋值给接口是需要装箱的.如下所示: class Program { static void Main(string[] args) { ISay catS ...

  10. CF576E

    *在#里发他一直WA这道CF题,然后我就去看了看,感觉还挺有趣的,那我就在这里整理一下我的思路..毕竟一边听歌.. 题意: 给个图,每条边初始无色,每次给一个询问(e,c)表示把e涂成颜色c,如果此时 ...