此笔记由菜鸟教程提供
0.
下载地址:https://nodejs.org/en/download/
node -v / node -- version 查看版本
node 进入node模式(在cmd中运行,REPL(Read Eval Print Loop:交互式解释器) ctrl + c退出
第一个服务器
const http = require("http");
http.createServer(function (request,response) {
// 服务器对访问浏览器的响应头
response.writeHead(200, {‘Content-Type‘: ‘text/plain‘});
// 服务器对访问浏览器的响应数据
response.end(‘Hi World\n‘);
}).listen(8888);
console.log(‘Server running at http://127.0.0.1:8888/‘); //在浏览器访问127.0.0.1:8888
1.模块
①.node自带:
(1)http(开启服务): http.createServer() 方法创建服务器; request, response 参数来接收和响应数据
(2)fs(文件系统): 像操作window的文件和文件夹,执行增删改查
②.node需引入
2.npm
NPM是随同NodeJS一起安装的包管理工具,常见的使用场景有以下几种:
允许用户从NPM服务器下载别人编写的第三方包到本地使用。
允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用。
允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用。
①.npm -v npm 版本
②.安装
npm install 模块名 本地安装模块
npm install 模块名 -g 全局安装模块
express@4.13.3 node_modules/express
③.查看
npm list -g 所有全局安装的模块
④.卸载
npm unstall 模块名 卸载模块
npm ls 是否卸载
⑤.更新
npm update 模块名 更新模块
⑥.探索
npm search 模块名 探索有模块名(模糊查询)
⑦.分布
npm publish 分布模块
3.回调函数
4.事件循环

执行异步操作的函数将回调函数作为最后一个参数, 回调函数接收错误对象作为第一个参数
var fs = require("fs");
fs.readFile(‘input.txt‘, function (err, data) {
if (err){
console.log(err.stack);
return;
}
console.log(data.toString());
});
console.log("程序执行完毕");
//不存在input.txt就报错
cmd模式node不能运行node.js文件不然报错

原文:https://www.cnblogs.com/lgyong/p/10421749.html