- 代码检查 eslint
npm i eslint -g
eslint --init
eslint 会创建一个 .eslintrc.json 的配置文件,同时自动安装并添加相关的模块到 devDependencies。
这里我们使用 Standard 规范,其主要特点是不加分号。
- 创建 .gitignore 文件,忽略提交的文件或目录
$ touch .gitignore
.gitignore 中常用内容:
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
- 创建 .editorconfig ,统一代码风格
$ touch .editorconfig
.editorconfig常用内容设置:
# editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
tab_width = 2
[*.md]
trim_trailing_whitespace = false
[Makefile]
indent_style = tab
=======

* [Node.js 系列教程 Node Hero:](https://github.com/simplefeel/react-node-learn)
* [Node.js 系列文章:](http://blog.fens.me/series-nodejs/)
* [零基础十分钟教你用Node.js写生成器:](https://github.com/i5ting/write-scaffold-with-nodejs)
* [Awehunt: ](https://awehunt.com/?tnid=5af1c0b67b4fac67bf3af2b4)
## nodejs web 框架
- [nodejs web 框架](https://awehunt.com/?tnid=5af2852253b5378301378d77)
- [nodejs github: ](https://github.com/nodejs/node)
* [免费域名解析: ](http://www.freenom.com/en/index.html) 最后不免费
[email protected]
* [汇智网: ](http://www.hubwiz.com/course/?type=Node.js)
* [Facebook 最热门的开源项目推荐!](https://www.itcodemonkey.com/article/1293.html)
* [WebSocket:5分钟从入门到精通](http://mp.weixin.qq.com/s?__biz=MjM5NTEwMTAwNg==&mid=2650213647&idx=1&sn=70467e0ff23500e0498c8fc21775d96e&chksm=befe0d2e8989843883321dbd9c3699218af1f8d4fa1fdbc7560fde9eb985309ab89df386a870&mpshare=1&scene=23&srcid=0108AEvqPGC8sson91HyRNSi#rd)
* [Node.js 原生开发入门完全教程(上)](http://mp.weixin.qq.com/s?__biz=MjM5NTEwMTAwNg==&mid=2650210826&idx=1&sn=230b49e75eaa6ad866174298a33dfdc5&chksm=befe022b89898b3d406e42202fb3f2f8fb863c6ff2b720e25b329969d8a5a2db339ed1a6cd6e&scene=21#wechat_redirect)
* [nodejs api v8.9.3 文档](http://nodejs.cn/api/)
* [阮一峰 nodejs 教程](http://javascript.ruanyifeng.com/nodejs/assert.html)
* [带你入门Nodejs](https://nodelover.me/course/nodejs)
* [node modules: ](https://github.com/node-modules)
* [深入理解Node.js:核心思想与源码分析:](https://github.com/yjhjstz/deep-into-node)
* [awesome-nodejs](https://github.com/sindresorhus/awesome-nodejs) : https://github.com/sindresorhus/awesome-nodejs
* [awesome-micro-npm-packages: ](https://github.com/parro-it/awesome-micro-npm-packages)
============================
* [node-orm2: ](https://github.com/dresende/node-orm2)
- [bookshelf:](https://github.com/bookshelf/bookshelf)
- [sequelize](https://github.com/sequelize/sequelize)
## Node.js 书籍
### 《Node.js 调试指南》
- [node-in-debugging:](https://github.com/nswbmw/node-in-debugging)
### [《Node.js 实战》 源码: ](https://github.com/MiYogurt/nodejs_shizhan)
- [《Node.js实战》(node-in-action) book: ](https://github.com/SFantasy/node-in-action)
- [gitbook: ](https://sfantasy.gitbooks.io/node-in-action/content/zh/)
### 《Node.js 包教不包会》 -- by alsotang
- [gitbook: ](https://xikong1995.gitbooks.io/nodejs/content/)
- [github: ](https://github.com/alsotang/node-lessons)
### 《Node.js 硬实战》
- [alexyoung/nodeinpractice](https://github.com/alexyoung/nodeinpractice)
### 实用Node.js,简单粗暴,新手学习的最短曲线
- [practical-node-tutorial: ](https://github.com/dev-reading/practical-node-tutorial)
## nodejs 模板
- [pug(原名 jade): ](https://github.com/pugjs/pug)
- [ejs : ](https://github.com/tj/ejs)
## [「node.js能开发大型网站吗?」](https://www.zhihu.com/question/21176891/answer/81862847)
## [node 调试:](http://i5ting.github.io/node-debug-tutorial/)
### A neat wrapper around node.js fs.watch / fs.watchFile / fsevents. https://paulmillr.com
- [github: ](https://github.com/paulmillr/chokidar)
### Inquirer.js A collection of common interactive command line user interfaces: 一组通用的交互式命令行用户界面
- [Inquirer.js](https://github.com/SBoudrias/Inquirer.js)
### Excel Workbook Manager: Excel工作簿管理器
- [exceljs:](https://github.com/guyonroche/exceljs)
### npm
[email protected] fairy
### demo 学习
N-blog: https://github.com/nswbmw/N-blog/tree/master/book https://github.com/tywei90/CMS-of-Blog_Production gitbook: https://maninboat.gitbooks.io/n-blog/content/
n-chat: https://github.com/nswbmw/N-chat
mystudy-blog: https://github.com/fairyly/mynodedemo/tree/myblog-node-addcomment
### 为node-gyp配置安装python2.7以及VC++ build Tools依赖
方法一 同样使用npm
npm install --global --production windows-build-tools
* candybox:https://candy.one/i/1205413
* [轻量级实时 Node.js 内核性能监控分析工具 ](http://easy-monitor.cn/index)
使用NPM下载和发布代码时都会接触到版本号。NPM使用语义版本号来管理代码,这里简单介绍一下。
语义版本号分为X.Y.Z三位,分别代表主版本号、次版本号和补丁版本号。当代码变更时,版本号按以下原则更新。
+ 如果只是修复bug,需要更新Z位。
+ 如果是新增了功能,但是向下兼容,需要更新Y位。
+ 如果有大变动,向下不兼容,需要更新X位。
- https://blog.csdn.net/u012422829/article/details/52760981
* Node 特点:
- 异步 I/O
- 事件与回调函数
- 单线程
- 跨平台
* Node 应用场景
- I/O 密集型
- CPU 密集型
* CommonJS 的模块规范
- 模块引入
- 模块定义
- 模块标识
* Node 引入模块
- 路径分析
- 文件定位
- 编译执行
Node 模块分为两类: Node 提供的模块-核心模块;用户编写的模块-文件模块;
* 模块编译
- JavaScript 编译会在头部添加 `(function (exports, require, module, __filename, __dirname) {\n,在尾部添加了\n})`
在编译的过程中,Node对获取的JavaScript文件内容进行了头尾包装。在头部添加了(function (exports, require, module, __filename, __dirname) {\n,在尾部添加了\n});。一个正常的JavaScript文件会被包装成如下的样子:
(function (exports, require, module, __filename, __dirname) { var math = require('math'); exports.area = function (radius) { return Math.PI * radius * radius; }; });
## node 异步 I/O
- 事件循环
- 观察者
- 请求对象
- I/O 线程池
## 非 I/O 异步 API
- 定时器 (setTimeout(),setInterval(),setImmediate())
- process.nextTick()
process.nextTick()的意思就是定义出一个动作,并且让这个动作在下一个事件轮询的时间点上执行
function foo() {console.error('foo');}process.nextTick(foo);console.error('bar');
也可以使用setTimeout()函数来达到貌似同样的执行效果:
setTimeout(foo, 0); console.log('bar');
但在内部的处理机制上,process.nextTick()和setTimeout(fn, 0)是不同的,process.nextTick()不是一个单纯的延时,他有更多的 特性。 更精确的说,process.nextTick()定义的调用会创建一个新的子堆栈。在当前的栈里,你可以执行任意多的操作。但一旦调用netxTick,函数就必须返回到父堆栈。 然后事件轮询机制又重新等待处理新的事件,如果发现nextTick的调用,就会创建一个新的栈。
* Node提供以下几个全局对象
global:表示Node所在的全局环境,类似于浏览器的window对象。需要注意的是,如果在浏览器中声明一个全局变量,实际上是声明了一个全局对象的属性,比如var x = 1等同于设置window.x = 1,但是Node不是这样,至少在模块中不是这样(REPL环境的行为与浏览器一致)。在模块文件中,声明var x = 1,该变量不是global对象的属性,global.x等于undefined。这是因为模块的全局变量都是该模块私有的,其他模块无法取到。
process:该对象表示Node所处的当前进程,允许开发者与该进程互动。
console:指向Node内置的console模块,提供命令行环境中的标准输入、标准输出功能。
* Node还提供一些全局函数。
setTimeout():用于在指定毫秒之后,运行回调函数。实际的调用间隔,还取决于系统因素。间隔的毫秒数在1毫秒到2,147,483,647毫秒(约24.8天)之间。如果超过这个范围,会被自动改为1毫秒。该方法返回一个整数,代表这个新建定时器的编号。 clearTimeout():用于终止一个setTimeout方法新建的定时器。 setInterval():用于每隔一定毫秒调用回调函数。由于系统因素,可能无法保证每次调用之间正好间隔指定的毫秒数,但只会多于这个间隔,而不会少于它。指定的毫秒数必须是1到2,147,483,647(大约24.8天)之间的整数,如果超过这个范围,会被自动改为1毫秒。该方法返回一个整数,代表这个新建定时器的编号。 clearInterval():终止一个用setInterval方法新建的定时器。 require():用于加载模块。 Buffer():用于操作二进制数据。
* Node提供两个全局变量,都以两个下划线开头。
__filename:指向当前运行的脚本文件名。 __dirname:指向当前运行的脚本所在的目录。
* 核心模块
http:提供HTTP服务器功能。 url:解析URL。 fs:与文件系统交互。 querystring:解析URL的查询字符串。 child_process:新建子进程。 util:提供一系列实用小工具。 path:处理文件路径。 crypto:提供加密和解密功能,基本上是对OpenSSL的包装。
* nodejs 接收 base64 转二进制存储
/* node.js app.post('/upload', function(req, res){ //接收前台POST过来的base64 var imgData = req.body.imgData; //过滤data:URL var base64Data = imgData.replace(/^data:image/\w+;base64,/, ""); var dataBuffer = new Buffer(base64Data, 'base64'); fs.writeFile("image.png", dataBuffer, function(err) { if(err){ res.send(err); }else{ res.send("保存成功!"); } }); }); */