什么是Node.js
Node.js是一个平台。大家可以使用Node.js来创建服务器,对网页开发人员非常方便。
Node.js 下载
软件包与源代码下载网站https://nodejs.org/en/download/
Node.js 创建服务器
//载入http模块
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
// 响应数据 "Hello World"
response.end('Hello World\n');
//8888为服务器端口
}).listen(8888);
// 终端打印启动信息
console.log('Server running at http://127.0.0.1:8888/');
REPL(Read Eval Print Loop)交互式解析器
在命令行中输入node,然后按输入键,便可以输入代码并立即执行
文件系统模块
在main.js读取data.txt的内容
事件模块
EventEmitter封装了事件触发和事件监听器。
观察者模式:一个对象被更改后便会自动通知依赖它的对象
下面的例子使用了2种创建事件的方法
总结
本文使用的模块有
1)http
2)fs 即fileSystem
3)events事件模块