了不起的Node.js:将JavaScript进行到底

书名:SMASHING Node.js : JavaScript Everywhere

原作者:(美)劳奇 Rauch.G

译者:赵静

出版日期:2014.1

Node中的JavaScript

1. global对象

在浏览器中,全局对象指的是window对象。在window对象上定义的所有内容(属性和函数)都可以被全局访问到。

setTimeout  === window.setTimeout
document === window.document

Node中的2个全局对象:

  • global--类似window对象,global对象上的属性都可以被全局访问到
  • process-所有全局执行上下文中的内容都在preocess对象中。

2. 模块系统

Node内置了很多实用的模块作为基础工具,包括http、net、fs等。

Node摒弃了采用定义一堆全局变量的方式,转而引入了一个简单却强大无比的模块系统,该模块系统有3个核心的全局对象:requiremoduleexports

绝对模块

绝对模块是指Node通过在其内部node_modules查找到的模块,或者Node内置的如fs这样的模块。

require('http');
require('fs');

直接通过名字来require这个模块,无须添加路径名的,就是绝对模块。

对于使用npm来安装的模块,例如安装colors模块,当安装完毕后,其路径就变成了./node_modules/colors

可以直接通过名称来require。

require('colors);

这种情况也是绝对模块。

相对模块

相对模块是将require指向一个相对工作目录中的js文件。例如

require('./src/module_a');
require('./src/module_b');

暴露API

暴露一个对象

在默认情况下,每个模块都会暴露出一个空对象。可以使用exports来暴露对象的属性和函数。

// module_a.js
exports.name = 'john';
exports.data = 'this is some data'; var privateVar = 5; exports.getPrivate = function(){
return privateVar;
}

在main.js 中调用

var a = require('./src/module_a');

console.log(a.name); //john
console.log(a.data); // this is some data
console.log(a.getPrivate()); // 5

暴露一个构造函数

person.js

module.exports = Person; // 对module.exports重写

function Person(name){
this.name = name;
} Person.prototype.talk = function(){
console.log('my name is' , this.name);
}

main.js

var Person = require('./src/person');

var John = new Person('John');
John.talk(); // my name is John

事件

在Node中事件的监听和分发使用EventEmit,定义了on 、 emit、once等方法。

使用EventEmitter

var EventEmitter = require('events');
var a = new EventEmitter();
a.on('event', function(){
console.log('event called.');
}); a.emit('event');

让自定义的类也可以使用事件监听,需要继承自EventEmitter。

使用原型继承方式

修改person.js

module.exports = Person;

function Person(name){
this.name = name;
} const EventEmitter = require('events');
Person.prototype = new EventEmitter; Person.prototype.talk = function(){
console.log('my name is' , this.name);
}

在main.js中调用

var Person = require('./src/person');

var John = new Person('John');
John.talk();
John.on('sleeping', function(){
console.log('sleeping called.')
}); John.emit('sleeping');

ES6继承方式

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event');

3. 文件系统

Node中使用fs模块操作文件。

同步读取文件

var fs = require('fs');

// 同步获取当前目录的文件列表
console.log(fs.readdirSync('.'));

异步读取文件(推荐方式)

var fs = require('fs');
// 异步的方式
function async(err, files){
console.log(files);
} fs.readdir(__dirname, async);

流对象

process全局对象中包含了3个流对象,分别对应3个Unix标准流。

  • stdin 标准输入
  • stdout 标准输出
  • stderr 标准错误

当前工作路径

process.cwd();
// or
__dirname

监视文件变化

使用fs.watchFile 或fs.watch监视文件变化

var fs = require('fs');
function callBackFiles(err, files){
files.forEach(function(file){
// 监听后缀是css的文件
if(/\.css/.test(file)){
fs.watchFile(process.cwd() + '/' + file, function(){
console.log(' - ' + file + ' changed!');
});
}
});
}
var files = fs.readdir(process.cwd(), callBackFiles);

当修改index.css文件并保存后,控制台将输出。

最新文章

  1. Centos 与本地终端 上传、下载 文件
  2. mongochef如何链接有权限的mongodb3.x数据库
  3. 8.0 Qweb 报表编写步骤
  4. 保存恢复临时信-Android 中使用onSaveInstanceState和onRestoreInstanceState
  5. NODEjs常见错误检查
  6. 【九度OJ】题目1201-二叉排序树
  7. laravel扩展xls处理maatwebsite/excel
  8. dede修改templets模板文件夹后,出现“无法在这个位置找到: ”错误的解决办法
  9. Redis进阶实践之二如何在Linux系统上安装安装Redis
  10. MYSQL之视图、触发器、存储过程、函数、事物、数据库锁和数据库备份
  11. 用Docker解决坑爹的环境搭建系列——mysql:5.6
  12. js 回调函数理解
  13. jQuery获取元素的方法
  14. VUE组件汇总
  15. 列表转换为字典(setdefault())
  16. .Net Core 在 Linux-Centos上的部署实战教程(二)
  17. jqprint控件使用
  18. Delphi窗体置顶及失去焦点后取得焦点
  19. EF-记录程序自动生成并执行的sql语句日志
  20. deeplearning 源码收集

热门文章

  1. linux extglob模式 和rm反选,除了某个文件外的其他文件全部删除的命令
  2. flask框架上下文
  3. Spring AOP注解配置demo
  4. Linux架构--------Rsync守护进程推和拉
  5. 一、创建并打包Cordova的App工程
  6. for循环性能测试
  7. js用逗号分隔字符串,保留双引号中的字符串
  8. 交互输入与for语句
  9. spring 知识结构
  10. springboot + 注解 + 拦截器 + JWT 实现角色权限控制