Review
- 2023-02-18 15:39
- 2024-07-20
[!Summary] Asynchronous Non-blocking Event-Driven
一、Introduction #
NodeJS is an open source, ==cross-platform== runtime environment and library that is used for running web applications outside the client’s browser. Node.js was written initially by Ryan Dahl in 2009.
It is used for server-side programming, and primarily deployed for non-blocking, event-driven servers, such as traditional web sites and back-end API services, but was originally designed with real-time, push-based architectures in mind. Every browser has its own version of a JS engine, and ==NodeJS is built on Google Chrome’s V8 JavaScript engine==. A NodeJS app runs in a single process, without creating a new thread for every request.1
单线程事件模型的优势
- 高并发性能:无线程上下文切换开销,I/O密集型任务友好,事件驱动
- 编程模型简单:无需考虑多线程同步、锁等复杂问题
- 内存占用低:单线程减少了线程创建和维护的开销
- 事件循环高效:基于 V8 引擎
- 适合 I/O 密集型任务
二、源码调试 #
==for macOS==
git clone https://ghproxy.com/https://github.com/nodejs/node.git
cd node
git checkout remotes/origin/v12.x调试工具
XCodeVSCode
#!/usr/bin/env node
// node.js script demo
// your code三、应用场景 #
- Real-time chats
- Internet of Things (IoT)
- Complex SPAs (Single-Page Applications)
- Real-time collaboration tools
- Streaming applications
- MicroServices architecture
3.1、NodeJS vs Browser #
Both the browser and Node.js use JavaScript as their programming language. Building apps that run in the browser is entirely different than building a Node.js application. Even though it’s always JavaScript, some key differences make the experience radically different.
In the browser, most of the time what you are doing is interacting with the DOM, or other Web Platform APIs like Cookies. Those do not exist in Node.js, of course. You don’t have the document, window and all the other objects that are provided by the browser.
And in the browser, we don’t have all the nice APIs that Node.js provides through its modules, like the filesystem access functionality.
Another big difference is that in Node.js you control the environment. Unless you are building an open source application that anyone can deploy anywhere, you know which version of Node.js you will run the application on. Compared to the browser environment, where you don’t get the luxury to choose what browser your visitors will use, this is very convenient.
This means that you can write all the modern ES2015+ JavaScript that your Node.js version supports. Since JavaScript moves so fast, but browsers can be a bit slow to upgrade, sometimes on the web you are stuck with using older JavaScript / ECMAScript releases. You can use Babel to transform your code to be ES5-compatible before shipping it to the browser, but in Node.js, you won’t need that.
Another difference is that Node.js supports both the CommonJS and ES module systems (since Node.js v12), while in the browser we are starting to see the ES Modules standard being implemented.
In practice, this means that you can use both require() and import in Node.js, while you are limited to import in the browser.
Node.js ES Support Node Green
四、Learning #
- NODESCHOOL https://nodeschool.io/
- Node API https://nodejs.org/api/
- Learn Node https://nodejs.org/en/learn/getting-started/introduction-to-nodejs
五、Development #
node --watch index.js
npm i -g nodemon
nodemon index.jsdebug NodeJS Application
node --inpsect index.jsSome other options for debugging are:
六、Deployment #
PM2 https://pm2.keymetrics.io/
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
npm i -g pm2 -g七、CheatSheet #
八、周边 #
Connect is an extensible HTTP server framework for nodejs using “plugins” known as middleware.
Reference #
- It’s All About NodeJS: https://dev.to/ozakaran/it-s-all-about-nodejs-37k4
- Keymetrics: https://keymetrics.io/
- Swagger – The World’s Most Popular Framework for APIs.: https://swagger.io/
- Passport: http://passportjs.org/
- Alexa: http://www.alexa.com/
- 玩转Nodejs日志管理log4js | 粉丝日志: http://blog.fens.me/nodejs-log4js/
- Practical Node.js: https://github.com/azat-co/practicalnode
- Now – Realtime global deployments: https://zeit.co/now
- Certbot: https://certbot.eff.org/#ubuntuxenial-nginx
- SSL Server Test: www.honeymorning.com (Powered by Qualys SSL Labs): https://www.ssllabs.com/ssltest/analyze.html?d=www.honeymorning.com
- 您的第一个 Progressive Web App | Web | Google Developers: https://developers.google.com/web/fundamentals/getting-started/codelabs/your-first-pwapp/?hl=zh-cn
- 客户端实现GitHub直接登录 · Issue #37 · TakWolf/CNode-Material-Design: https://github.com/TakWolf/CNode-Material-Design/issues/37
- Express 4.x API - 作业部落 Cmd Markdown 编辑阅读器: https://www.zybuluo.com/XiangZhou/note/208532
- 重定向 CORS 跨域请求 | Harttle Land: http://harttle.com/2016/12/30/cors-redirect.html
- Nodejs使用session、cookie进行登录验证功能的实现 | 但丁笔记 –一个理想主义文青的IT自留地: http://www.5941740.cn/2016/06/08/node-authority/
- 登录那些事儿 - CNode技术社区: https://cnodejs.org/topic/5671441a1d2912ce2a35aaa1
- Trello: https://trello.com/
- Apollo Server 1.0 — A GraphQL Server for all Node.js frameworks: https://dev-blog.apollodata.com/apollo-server-1-0-a-graphql-server-for-all-node-js-frameworks-2b37d3342f7c
- bcryptjs: https://www.npmjs.com/package/bcryptjs
- Nodemailer: https://nodemailer.com/about/
- Send email using nodejs and express in 5 simple steps | JavaScript: http://javascript.tutorialhorizon.com/2015/07/02/send-email-node-js-express/
- (99+) nodejs - Google 网上论坛: https://groups.google.com/forum/#!forum/nodejs
- freenode: https://freenode.net/
- Inspect with Chrome Developer Tools: chrome://inspect/#devices
- Node.js Frameworks: http://nodeframework.com/
- cookie 和 session - Node.js 实战心得 - 极客学院Wiki: http://wiki.jikexueyuan.com/project/node-lessons/cookie-session.html
- 加速electron在国内的下载速度 | Tomyail的记忆现场: http://blog.tomyail.com/install-electron-slow-in-china/
- How to Build and Structure a Node.js MVC Application — SitePoint: https://www.sitepoint.com/node-js-mvc-application/
- NodeJS 区块链: http://www.darrenbeck.co.uk/blockchain/nodejs/nodejscrypto/
- Express: http://expressjs.com/
- nodejs设置启动端口在不同系统下的写法_w3cmark: http://www.w3cmark.com/2016/436.html
- Node.js开发实战 https://time.geekbang.org/course/intro/100036001?tab=catalog
- 15 个常见的 Node.js 面试问题及答案
- Introduction to Node.js
- History of Node.js on a Timeline
- How Did Node.js Come About?
aaa ↩︎