1. jscript顶层对象window,node顶层对象是global对象;
var a=100;
global.a=undefined;
2. nodejs中模块的概念 一个文件就是一个模块,每个模块都有自己的作用域;
console.log(__filename);
require('./');
require('./apple');
3. 模块加载-变量的使用
- 访问方式:全局挂载:
global.a=100;
- 使用模块对象 :require('./apple.js')方法会返回exports
console.log(module);
{
id:'',
exports:{},
parent:null,
filename:'',
children:[{id="",exports:{},parent,filename...}],
}
module.exports===exports;
module.exports.apple="乔布斯";
module.exports=[1,2,3];
module.exports.a=6;
exports推荐挂载属性,不推荐直接赋值
4. 常用API----global
__filename:'';
__filename:'';
Date();
setTimeOut();
setInterval();
5. 其它常用API
global.process//全局对象
console.log(process);
console.log();
process.stdin、process.stdout 标准输入输出流 (IO)
let a;
let b;
process.stdin.resume();
process.stdout.write('【心动时刻】爱情保鲜测量仪...'+'请输入你的名字:');
process.stdin.on("data",(chunk)=>{
if(!a){
a=chunk;
process.stdout.write('请输入你爱人的名字:');
}else{
b=chunk;
process.stdout.write(a+'@'+b+'你们的爱情保鲜一万年!');
}
});
process.argv(node,.js文件名,以此是命令行参数) //包含命令行参数的数组
1. node.exe;
2. 当前.js文件名;
3. 接下来依次是命令行参数
process.execPath 开启当前进程的node的绝对路径
process.env 用户环境信息
process.version node版本
process.versions node以及node依赖包的版本信息
process.pid 当前进程pid
process.title 进程显示名称(getter/setter)
process.arch cpu处理器架构 arm/ia32/x64
process.latform 操作系统
process.cwd 进程工作目录
process.chdir 改变当前进程的工作目录
process.memoryUsage() 内存使用情况 byte
process.exit(code)
process.kill(pid);
- Buffer 全局类,文件读取,操作二进制数据流,网络数据读取
new Buffer(size);
new Buffer([1,2,3]);
new Buffer(string,encoding);
var bf=new Buffer('miaoV','utf-8');
for(var i=0;i<bf.length;i++){
console.log(bf[i].tostring(16));
console.log(String.fromCharCode(bf[i]));
}
var str1='miaov';
var bf1=new Buffer(str1);
console.log(str1.length);
console.log(bf1.length);
console.log(str1[0]);
var str2="妙味";
var bf2=new Buffer(str2);
console.log(str2.length);
console.log(bf2.length);
var buf=new Buffer(1024);
buf.write(string,offset起始,length长度,encoding);
buf.tostring('utf-8',1,2);
buf.slice(start,end);
buf.copy();
buf.copy(目标buf,新buf start,新buf end);
Buffer.isEncoding('utf-8');
Buffer.isBuffer();
- File System文件系统
var fs=require('fs');//引入文件系统
/**
* 异步方法,通过callback继续操作 非阻塞
* path 路径
* flags 打开方式 读/写 r/w
* mode 文件模式 读/写/执行 4/2/1
* callback (err,fd) 错误信息,文件标识,定时器
*/
fs.open(path,flags,[mode],callback); //路径 打开方式读、写
eg: fs.open('1.txt','r',function);
/**
* 同步方法,通过返回值继续操作 阻塞
*/
fs.openSync(path,flags,[mode]);
fs.open('1.txt','r',function(err,fd){
fs.read(fd,new Buffer(10),offset bf偏移量,length内容长度,position 读取fd起始值,function(err,bf.length写入长度,bf){
});
})
/**
* 打开文件之后读取文件内容
* fd
* bf
* offset bf起始位置
* length 读取长度
* position 从fd第几位开始
* callback (err,bf.length,bf)
*/
fs.read();
/**
* 同步读取
*/
fs.readSync();
/**
* 打开文件之后读取文件内容 open模式为 'r+'模式
* fd 开发返回的fd
* bf 需要写入的bf
* offset bf起始位置
* length 读取长度
* position 从fd第几位开始
* callback (err,bf.length,bf)
*/
fs.write(fd,bf,offset,length,fd的起始,callback)
/**写字符串
*/
fs.write(fd,bf,offset,length,fd的起始,callback);
fs.close(fd,function(){
});
//不用open 会覆盖
fs.writeFile(filename,string,callback);
fs.writeFileSync();
//追加,会新建
fs.appendFile(filename,string)
fs.exists(filename,callback)
fs.readFile
fs.unlink
fs.raname
fs.stat 文件信息
fs.watch 观察路径改变
fs.mkdir(path,function)
fs.rmdir
fs.readdir