前端node.js常见语法记录

1.node.js组成部分

  1. 引入 required 模块:我们可以使用 require 指令来载入 Node.js 模块
var http = require("http");
  1. 创建服务器:服务器可以监听客户端的请求,类似于 Apache 、Nginx 等 HTTP 服务器
  2. 接收请求与响应请求 服务器很容易创建,客户端可以使用浏览器或终端发送 HTTP 请求,服务器接收请求后返回响应数据。

2.模块系统

Node.js 提供了 exports 和 require 两个对象,其中 exports 是模块公开的接口,require 用于从外部获取一个模块的接口,即所获取模块的 exports 对象。

2.1引入模块(require)

var hello = require('./hello');
hello.world();

2.2公开模块(exports)

// hello.js 
exports.world = function() {
  console.log('Hello World');
}

2.3exports和module.exports的区别

module.exports使用方式:

//hello.js 
function Hello() { 
    var name; 
    this.setName = function(thyName) { 
        name = thyName; 
    }; 
    this.sayHello = function() { 
        console.log('Hello ' + name); 
    }; 
}; 
module.exports = Hello;

//main.js 
var Hello = require('./hello'); 
hello = new Hello(); 
hello.setName('BYVoid'); 
hello.sayHello(); 

在这里插入图片描述上图可以看到在node.js中给js文件内置了exports及module两个对象,modul包含了exports等属性。并且在初始时两个对象相等,代表了两个对象指向同一块内存。
在这里插入图片描述
上图表示,exports和module.exports都有了haha对象,exports是module.exports的一个引用,exports指向的是module.exports
在这里插入图片描述
但是当修改module.exports指向后,两者将不在相等

2.4exports 和 module.exports 的使用

  1. 如果要对外暴露属性或方法,就用 exports 就行,要暴露对象(类似class,包含了很多属性和方法),就用 module.exports。
  2. 不建议同时使用 exports 和 module.exports。如果先使用 exports 对外暴露属性或方法,再使用 module.exports 暴露对象,会使得 exports 上暴露的属性或者方法失效。

3.常见内置模块

3.1 全局对象

  1. __filename 表示当前正在执行的脚本的文件名
  2. __dirname 表示当前执行脚本所在的目录
    在这里插入图片描述

3.2 events 模块(事件监听器)

events 模块只提供了一个对象: events.EventEmitter。EventEmitter 的核心就是事件触发与事件监听器功能的封装。

  • events.EventEmitter
//EventBus.js

var events = require('events');

//定义对象并导出,存储所有事件名称,EventEmitter处理中心
export const eventBus = {}
eventBus.bus = new events.EventEmitter();

//配置所有的事件名称
//地图加载完毕事件
eventBus.MAP_IS_LOAD = 'MAP_IS_LOAD';
//测量坐标
eventBus.COORD = 'COORD';
//点选查询
eventBus.MOUSE_CLICK_QUERY = 'MOUSE_CLICK_QUERY';
//时态图层添加事件
eventBus.LAYER_TIMER_ADD = 'LAYER_TIMER_ADD';
//绘制对象
eventBus.DRAW_GRAPH = 'DRAW_GRAPH';
  • on ,addListener,once
    on和addListener:本质上没什么却别(on是注册一个监听器,addListener是添加一个监听器到监听器数组的尾部。)
    once:注册一个单次监听器,监听器最多只会触发一次
//某一个js监听某事件
import {eventBus} from "路径";

eventBus.bus.on(eventBus.MAP_IS_LOAD, function(message1,message2) { 
    console.log(message1); 
}); 
//支持若干个事件监听器。按顺序执行...
eventBus.bus.addListener(eventBus.MAP_IS_LOAD, function(message1,message2) { 
    console.log("第二个输出"); 
}); 

  • emit,removeListener,removeAllListeners
    emit:执行每个监听器,如果事件有注册监听返回 true,否则返回 false。
    removeListener:移除指定事件的某个监听器,两个参数:第一个是事件名称,第二个是回调函数名称。
    removeAllListeners:不传参移除所有事件的所有监听器。如果传事件参数,则移除指定事件的所有监听器。
//某一个js触发事件
import {eventBus} from "路径";

setTimeout(function() { //一秒后触发地图加载完毕事件
    eventBus.bus.emit(eventBus.MAP_IS_LOAD,"我是参数1","我是参数二"); 
}, 1000); 

setTimeout(function() { //一秒后触发地图加载完毕事件
	eventEmitter.removeAllListeners(eventBus.MAP_IS_LOAD);
}, 2000); 

3.3 fs 模块(文件系统)

const fs = require('fs')

1.读取文件(readFile,readFileSync)
读取文件内容两种方式:异步readFile,同步readFileSync

var fs = require("fs");

// 异步读取
fs.readFile('input.txt', function (err, data) {
   if (err) {
       return console.error(err);
   }
   console.log("异步读取: " + data.toString());
});

// 同步读取
var data = fs.readFileSync('input.txt');
console.log("同步读取: " + data.toString());

3.写入文件(writeFile)

var fs = require("fs");

console.log("准备写入文件");
fs.writeFile('input.txt', '我是通 过fs.writeFile 写入文件的内容',  function(err) {
   if (err) {
       return console.error(err);
   }
   console.log("数据写入成功!");
   console.log("--------我是分割线-------------")
   console.log("读取写入的数据!");
   fs.readFile('input.txt', function (err, data) {
      if (err) {
         return console.error(err);
      }
      console.log("异步读取文件数据: " + data.toString());
   });
});

2.创建文件(mkdir)

var fs = require("fs");
// tmp 目录必须存在
console.log("创建目录 /tmp/test/");
fs.mkdir("/tmp/test/",function(err){
   if (err) {
       return console.error(err);
   }
   console.log("目录创建成功。");
});

3.4 path 模块(处理文件路径)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值