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.exportsmodule模块的真正接口, exports是指向它的变量;
  • 所以设置module.exportsexports设置就失效了

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. 每天写点shell——read的用法

    1.read基本读取 #!/bin/bash #testing the read command echo -n "Enter you name:" #echo -n 让用户直接在 ...

  2. Android常用组件之ListView

    使用ListAdapter是ListView的适配器. 使用ListView.setAdapter()方法来设置ListView的适配器. 简单的使用 adapter = new ArrayAdapt ...

  3. Greenplum-概念篇

    Greenplum主要组件包括:Master.Segments.Interconnect:其他组件包括ETL Server.Greenplum command center等.0. 组件之-Maste ...

  4. MyBatis框架在控制台打印Sql语句-遁地龙卷风

    第二版 (-1)写在前面 我用的是MyBatis 3.2.4,Maven Project (0)mybatis-config.xml <settings> <setting name ...

  5. MySQL中的常用工具

    一.mysql 客户端连接工具 二.myisampack MyISAM表压缩工具 三.mysqladmin MySQL管理工具 四.mysqlbinlog 日志管理工具 五.mysqlcheck My ...

  6. Sql Server函数全解<二>数学函数

    阅读目录 1.绝对值函数ABS(x)和返回圆周率的函数PI() 2.平方根函数SQRT(x) 3.获取随机函数的函数RAND()和RAND(x) 4.四舍五入函数ROUND(x,y) 5.符号函数SI ...

  7. QL Server 2008 所有账号丢失sysadmin权限,sa账号亦没有开启,该如何解决??

    1. 用Run as a administrator打开命令提示符里输入NET STOP MSSQLSERVER, 即停止MSSQLSERVER运行. 2. 在命令提示符里输入 NET START M ...

  8. mysql安装时到最后start service时就不响应了的解决方法

  9. LeetCode 299 Bulls and Cows

    Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...

  10. SQL多表查询,消除表中的重复的内容

    看到朋友再写一个SQL语句:两个表a1表中有SN.SN2.TN,b1表有SM.SM2.TN2,若a1的SN中的数据和b1的SM中的数据是一致的,那么将a1中对应的数据修改成b1中对应的数据. upda ...