【Node.js】官网学习笔记
Node.js 官网学习笔记
node 是什么?
-
开源跨平台的js运行环境
-
基于Chrome 的 V8 Js 引擎,性能强
-
asynchronous I/O + non-blocking ; 支持异步非阻塞IO
-
low-level platform,在此平台上建立了很多框架
- Meteor: An incredibly powerful full-stack framework, powering you with an isomorphic approach to building apps with JavaScript, sharing code on the client and the server. Once an off-the-shelf tool that provided everything, now integrates with frontend libs React, Vue, and Angular. Can be used to create mobile apps as well.
node 怎么用?
Hellow Word
文件命名为server.js
,并填充脚本:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
teminal里面执行
node server.js
node 在多版本中进行控制
nvm
https://github.com/nvm-sh/nvm
怎么用好node?
I would recommend you to have a good grasp of the main JavaScript concepts before diving into Node.js:
官网强调,要深入到node.js中,最好熟悉JS的以下特性
JS特性
- Lexical Structure
- Expressions
- Types
- Classes
- Variables
- Functions
- this
- Arrow Functions
- Loops
- Scopes
- Arrays
- Template Literals
- Semicolons
- Strict Mode
- ECMAScript 6, 2016, 2017
异步编程
- Asynchronous programming and callbacks
- Timers
- Promises
- Async and Await
- Closures
- The Event Loop
node 对js的兼容性考虑
I have my infrastructure set up to leverage the --harmony flag. Should I remove it?
The current behavior of the
--harmony
flag on Node.js is to enable staged features only. After all, it is now a synonym of--es_staging
. As mentioned above, these are completed features that have not been considered stable yet. If you want to play safe, especially on production environments, consider removing this runtime flag until it ships by default on V8 and, consequently, on Node.js. If you keep this enabled, you should be prepared for further Node.js upgrades to break your code if V8 changes their semantics to more closely follow the standard.
总的来说,建议除了shipping 至 V8的 的特性都不要用,具体的定义:
All ECMAScript 2015 (ES6) features are split into three groups for shipping, staged, and in progress features:
- All shipping features, which V8 considers stable, are turned on by default on Node.js and do NOT require any kind of runtime flag.
- Staged features, which are almost-completed features that are not considered stable by the V8 team, require a runtime flag:
--harmony
. - In progress features can be activated individually by their respective harmony flag, although this is highly discouraged unless for testing purposes. Note: these flags are exposed by V8 and will potentially change without any deprecation notice.
官网其他有意思的内容
Fork 和 merge 应用
把一个实验性的功能作为一个分支Fork出来,实验成功后又Merge进主干
2014
- The Big Fork: io.js is a major fork of Node.js, with the goal of introducing ES6 support and moving faster
2015
- The Node.js Foundation is born
- IO.js is merged back into Node.js
- npm introduces private modules
- Node.js 4 (versions 1, 2 and 3 never previously released)