01 Node Js Overview

Review

  1. 2023-02-18 15:39
  2. 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

单线程事件模型的优势

  1. 高并发性能:无线程上下文切换开销,I/O密集型任务友好,事件驱动
  2. 编程模型简单:无需考虑多线程同步、锁等复杂问题
  3. 内存占用低:单线程减少了线程创建和维护的开销
  4. 事件循环高效:基于 V8 引擎
  5. 适合 I/O 密集型任务

二、源码调试 #

==for macOS==

git clone https://ghproxy.com/https://github.com/nodejs/node.git 
cd node

git checkout remotes/origin/v12.x

调试工具

  • XCode
  • VSCode
#!/usr/bin/env node

// node.js script demo
// your code

三、应用场景 #

  1. Real-time chats
  2. Internet of Things (IoT)
  3. Complex SPAs (Single-Page Applications)
  4. Real-time collaboration tools
  5. Streaming applications
  6. 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 documentwindow 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 #

  1. NODESCHOOL https://nodeschool.io/
  2. Node API https://nodejs.org/api/
  3. Learn Node https://nodejs.org/en/learn/getting-started/introduction-to-nodejs

五、Development #

node --watch index.js


npm i -g nodemon
nodemon index.js

debug NodeJS Application

node --inpsect index.js

Some 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 #

  1. NodeJS Security Cheat Sheet
  2. Github NodeJS CheatSheet

八、周边 #

  1. async https://caolan.github.io/async/v3/
  2. connect https://github.com/senchalabs/connect

Connect is an extensible HTTP server framework for nodejs using “plugins” known as middleware.

Reference #

  1. It’s All About NodeJS: https://dev.to/ozakaran/it-s-all-about-nodejs-37k4
  2. Keymetrics: https://keymetrics.io/
  3. Swagger – The World’s Most Popular Framework for APIs.: https://swagger.io/
  4. Passport: http://passportjs.org/
  5. Alexa: http://www.alexa.com/
  6. 玩转Nodejs日志管理log4js | 粉丝日志: http://blog.fens.me/nodejs-log4js/
  7. Practical Node.js: https://github.com/azat-co/practicalnode
  8. Now – Realtime global deployments: https://zeit.co/now
  9. Certbot: https://certbot.eff.org/#ubuntuxenial-nginx
  10. SSL Server Test: www.honeymorning.com (Powered by Qualys SSL Labs): https://www.ssllabs.com/ssltest/analyze.html?d=www.honeymorning.com
  11. 您的第一个 Progressive Web App  |  Web  |  Google Developers: https://developers.google.com/web/fundamentals/getting-started/codelabs/your-first-pwapp/?hl=zh-cn
  12. 客户端实现GitHub直接登录 · Issue #37 · TakWolf/CNode-Material-Design: https://github.com/TakWolf/CNode-Material-Design/issues/37
  13. Express 4.x API - 作业部落 Cmd Markdown 编辑阅读器: https://www.zybuluo.com/XiangZhou/note/208532
  14. 重定向 CORS 跨域请求 | Harttle Land: http://harttle.com/2016/12/30/cors-redirect.html
  15. Nodejs使用session、cookie进行登录验证功能的实现 | 但丁笔记 –一个理想主义文青的IT自留地: http://www.5941740.cn/2016/06/08/node-authority/
  16. 登录那些事儿 - CNode技术社区: https://cnodejs.org/topic/5671441a1d2912ce2a35aaa1
  17. Trello: https://trello.com/
  18. 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
  19. bcryptjs: https://www.npmjs.com/package/bcryptjs
  20. Nodemailer: https://nodemailer.com/about/
  21. Send email using nodejs and express in 5 simple steps | JavaScript: http://javascript.tutorialhorizon.com/2015/07/02/send-email-node-js-express/
  22. (99+) nodejs - Google 网上论坛: https://groups.google.com/forum/#!forum/nodejs
  23. freenode: https://freenode.net/
  24. Inspect with Chrome Developer Tools: chrome://inspect/#devices
  25. Node.js Frameworks: http://nodeframework.com/
  26. cookie 和 session - Node.js 实战心得 - 极客学院Wiki: http://wiki.jikexueyuan.com/project/node-lessons/cookie-session.html
  27. 加速electron在国内的下载速度 | Tomyail的记忆现场: http://blog.tomyail.com/install-electron-slow-in-china/
  28. How to Build and Structure a Node.js MVC Application — SitePoint: https://www.sitepoint.com/node-js-mvc-application/
  29. NodeJS 区块链: http://www.darrenbeck.co.uk/blockchain/nodejs/nodejscrypto/
  30. Express: http://expressjs.com/
  31. nodejs设置启动端口在不同系统下的写法_w3cmark: http://www.w3cmark.com/2016/436.html
  32. Node.js开发实战 https://time.geekbang.org/course/intro/100036001?tab=catalog
  33. 15 个常见的 Node.js 面试问题及答案
  34. Introduction to Node.js
  35. History of Node.js on a Timeline
  36. How Did Node.js Come About?

  1. aaa ↩︎