cd命令
- 进入d盘
- 输入 d:
C:\Users\Administrator>D: D:\>
- 返回上一级目录
H:\网页前端\移动开发\第四章\第二章>cd .. H:\网页前端\移动开发\第四章>
包含文件名称的全路径
console.log(__filename);
文件的路径(不包含文件名称)
console.log(__dirname);
在Node.js中没有window对象,但是有一个类似的对象global,访问全局成员的时候可以省略global
global.console.log(123456);
REPL 环境
使用node 命令进入在REPL环境中,_表示最后一次执行结果; .exit 可以退出REPL环境
argv是一个数组,默认情况下,前两项数据分别是:Node.js环境的路径;当前执行的js文件的全路径
- 从第三个参数开始表示命令行参数
console.log(process.argv);
[ 'D:\\soft\\nodejs\\node.exe', 'H:\\网页前端\\移动开发\\第四章\\第二章\\02.js' ]
- 打印当前系统的架构(64位或者32位)
console.log(process.arch);
x64
引入模块
- var module = require(’./03.js’); 在当前的文件下加载03.js文件
03js文件内容
// 导出成员的另一种方式[如果成员较多可有使用module]
module.exports = function(){
console.log('hello');
};
04.js文件
var module = require('./03.js');
module();
如果要导出单个的成员或者比较少的成员,一般我们使用exports导出;如果要导出的成员比较多,一般我们使用module.exports的方式这两种方式不能同时使用**
03js文件内容
// 导出成员的另一种方式[如果成员较多可有使用module]
var sum = function(a,b){
return parseInt(a) + parseInt(b);
}
// 导出模块成员
exports.sum = sum;
04.js文件
var module = require('./03.js');
var ret = module.sum(12,13);
console.log(ret);
03js文件内容
// 导出成员的另一种方式[如果成员较多可有使用module]
var sum = function(a,b){
return parseInt(a) + parseInt(b);
}
// 导出模块成员
module.exports = sum;
04.js文件
var module = require('./03.js');
var ret = module(12,15);
console.log(ret);
引入对象
- 创建模块对象
/*
四则运算-成员导出方式分析
*/
var sum = function(a,b){
return parseInt(a) + parseInt(b);
}
var subtract = function(a,b){
return parseInt(a) - parseInt(b);
}
var multiply = function(a,b){
return parseInt(a) * parseInt(b);
}
var divide = function(a,b){
return parseInt(a) / parseInt(b);
}
// 导出成员
// exports.sum = sum;
// exports.subtract = subtract;
//和上边同理 变量名:函数名称
module.exports = {
sum : sum,
subtract : subtract,
multiply : multiply,
divide : divide
}
// module.exports = [1,2,3]; //覆盖
- 引入模块
/*
测试导出
*/
var m = require('./05.js');
console.log(m); //{} 如果没有定义方法则会返回{} 如果定义则会返回json对象
// var ret = m.sum(1,2);
// var ret1 = m.subtract(1,2);
// console.log(ret,ret1);
模块导出成员
模块成员导出:global
已经加载的模块会缓存
console.log('hello');
var flag = 123;
global.flag = flag; //必须加上global 否则undefined
- 模块导出
require('./07');
console.log(global.flag);
模块加载的三种情况
模块文件的后缀3种情况:.js .json .node
上述三种模块的加载优先级(不加文件后缀时的优先级):.js -> .json -> .node
- js文件
exports.showInfo = function(){
console.log('nihao');
}
- json 文件
{
"username":"张三",
"age":"12"
}
- node文件是二进制文件
var m = require('./data.json');
console.log(m.username); //张三
//node内部存在一个返回对象的方法函数
var m = require('./data.node');
console.log(typeof m); //object
var ret = m.hello(); //value
console.log(ret);
REPL read-eval-print-loop 读取代码-执行-打印结果-循环这个过程
Buffer
Buffer的基本操作 Buffer本质上就是字节数组 1、构造方法(类) 2、静态方法 3、实例方法
实例化
实例化buf对象
let buf = new Buffer(5); //弃用
let buf = Buffer.alloc(5); //声明buffer大小
console.log(buf);
form() 转换
let buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); //编码转换
console.log(buf.toString());
静态方法
console.log(Buffer.isEncoding(‘utf8’));
console.log(Buffer.isEncoding(‘gbk’));
默认编码
let buf = Buffer.from(‘hello’); //默认utf-8
console.log(Buffer.isBuffer(buf)); //true
console.log(Buffer.isBuffer({})); //false
- 指定编码
let buf = Buffer.from(‘中国’,‘ascii’); 指定ascii编码
console.log(Buffer.byteLength(buf)); //长度 2
console.log(buf.toString()); //默认utf-8 这里是乱码
concat()
let buf1 = Buffer.alloc(3);
let buf2 = Buffer.alloc(5);
let buf3 = Buffer.concat([buf1,buf2]);
console.log(Buffer.byteLength(buf3)); //8
- 合并
let buf1 = Buffer.from(‘tom’);
let buf2 = Buffer.from(‘jerry’);
let buf3 = Buffer.concat([buf1,buf2]);
console.log(Buffer.byteLength(buf3)); //8
console.log(buf3.toString()); //tomjerry
实例方法
let buf = Buffer.alloc(5);
buf.write(‘hello’,2,2); //5个长度在 下标2的位置起填充2个字符编码
console.log(buf); //<Buffer 00 00 68 65 00>
编码截取
let buf = Buffer.from(‘hello’);
let buf1 = buf.slice(2,4); //从下标位置2 从第一个元素到地4个元素的截取
console.log(buf === buf1);//false
console.log(buf1.toString()); // ll
// toJSON方法不需要显式调用,当JSON.stringify方法调用的时候会自动调用toJSON方法
// const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);
const buf = Buffer.from(‘hello’);
const json = JSON.stringify(buf);
console.log(json); //{“type”:“Buffer”,“data”:[104,101,108,108,111]}