Node.js 是一个开源跨平台的 JavaScript 运行环境,它是时下最流行的工具,能够应用于几乎所有的项目。Node.js 运行 V8 JavaScript engine。它是 chrome 的核心,这让 Node.js 可以脱离浏览器运行 JavaScript 代码。

一个 Node.js app 运行在一个线程中,不会给每个 request 建立一个进程。Node.js 在其标准库中提供了一个 asynchronous I/O primitives 异步 IO 原生语法库来防止阻塞,同时 Node.js 中的库使用一种 non-blocking paradigms 无阻塞范式,将阻塞作为一种 exception 来处理。

当 Node.js 执行 IO 动作,例如读取文件,访问数据库等,不同于等待进程,占用 CPU 资源,Node.js 会在收到 request 响应后再去处理后续操作。这将使 Node.js 可以在一个 server 上同时处理上千条链接而不会引入大量的进程而导致 bug 出现。

Node.js 的另一大优势是使用 JavaScript 的前端开发者可以同时编写 server 端的代码而不用学习新的语言。

最新的 ECMAScript 标准可以在 Node.js 中使用,你不需要等待用户去更新浏览器。通过使用不同版本的 Node.js 来切换不同的 ECMAScript 标准。

Node.js 使用 JavaScript 语言,如果你还不太了解 js 可以参考我之前的 8 篇 JavaScript 教程:JavaScript 入门教程之一 -- 总览

海量的第三方库

通过 npm 简单的结构帮助 Node.js 生态系统快速的增长。目前 npm 注册超过 1,000,000 个开源库可供免费使用。

hello world

web server 是最常见的 hello world 示例。

新建一个 js 文件,内容如下:

const http = require('http')

const hostname = '127.0.0.1'
const port = 8080

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}/`)
})

以上代码首先引入 http 模块。Node.js 使用 CommonJS module 系统,使用内建的 require 关键词来引入其他文件写好的 module 模块。

Node.js 有一个很好的标准库:https://nodejs.org/api/,包含有对 network 的直接支持。

createServer method 创建一个新的 http server 并返回它。定义了一个 requestListener function,会被自动添加到 request event。

listen method 定义特定的监听地址和端口。当 server 运行起来后, listen method 调用的 function 会被执行,这里我们显示 server 已启动的信息。

当收到一个 request,会自动调用 request event,并传入两个 object:一个 request (an http.IncomingMessage object) 和一个 response (an http.ServerResponse object)。

这两个 object 是处理 http 访问的核心。

request object 提供了 request 请求的信息,在以上示例中没有使用到这个,但是我们可读取 request header 和 data 数据。

response object 用来返回数据给发起请求者,在这里通过:

res.statusCode = 200

返回一个 200 状态码,表示一次成功的响应。

然后我们设置一个 header:

res.setHeader('Content-Type', 'text/plain')

最后我们关闭 response 响应,并将响应内容作为 end method 的参数传入:

res.end('Hello World\n')

Node.js Frameworks and Tools

Node.js 是一个底层平台,为了让开发更加容易,海量的基于 Node.js 的第三方库通过社区建立。它们中的很多已经非常流行,以下是一些常见的第三方库:

  • AdonisJs: A full-stack framework highly focused on developer ergonomics, stability, and confidence. Adonis is one of the fastest Node.js web frameworks.
  • Express: It provides one of the most simple yet powerful ways to create a web server. Its minimalist approach, unopinionated, focused on the core features of a server, is key to its success.
  • Fastify: A web framework highly focused on providing the best developer experience with the least overhead and a powerful plugin architecture. Fastify is one of the fastest Node.js web frameworks.
  • Gatsby: A React-based, GraphQL powered, static site generator with a very rich ecosystem of plugins and starters.
  • hapi: A rich framework for building applications and services that enables developers to focus on writing reusable application logic instead of spending time building infrastructure.
  • koa: It is built by the same team behind Express, aims to be even simpler and smaller, building on top of years of knowledge. The new project born out of the need to create incompatible changes without disrupting the existing community.
  • Loopback.io: Makes it easy to build modern applications that require complex integrations.
  • 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.
  • Micro: It provides a very lightweight server to create asynchronous HTTP microservices.
  • NestJS: A TypeScript based progressive Node.js framework for building enterprise-grade efficient, reliable and scalable server-side applications.
  • Next.js: React framework that gives you the best developer experience with all the features you need for production: hybrid static & server rendering, TypeScript support, smart bundling, route pre-fetching, and more.
  • Nx: A toolkit for full-stack monorepo development using NestJS, Express, React, Angular, and more! Nx helps scale your development from one team building one application to many teams collaborating on multiple applications!
  • Sapper: Sapper is a framework for building web applications of all sizes, with a beautiful development experience and flexible filesystem-based routing. Offers SSR and more!
  • Socket.io: A real-time communication engine to build network applications.
  • Strapi: Strapi is a flexible, open-source Headless CMS that gives developers the freedom to choose their favorite tools and frameworks while also allowing editors to easily manage and distribute their content. By making the admin panel and API extensible through a plugin system, Strapi enables the world's largest companies to accelerate content delivery while building beautiful digital experiences.

安装

在官方下载页面下载对应系统的安装包:https://nodejs.org/en/download/

macOS 可以通过 brew 来安装:

brew install node

node.js 和浏览器端的区别

浏览器端和 node.js 都是使用 JavaScript 作为编程语言,但他们是完全不同的。

node.js 可以同时开发前端和后端 application,这样使用一种语言就可以完成所有的开发。

浏览器中使用 DOM 结构,或者其他 web 平台的 API 如:cookies,这些在 node.js 中都是没有的,documentwindow 等这些 object 是没有的。

使用 node.js 由你来控制 environment,你知道 application 所使用的 node.js 是哪个版本的,相比较于浏览器,你并不知道用户使用了什么浏览器。所以你可以使用更加现代的 ES 标准来编写程序。

另一个区别是 node.js 使用 CommonJS 模块系统,浏览器端我们已经可以使用 ES 的标准命令来导入模块了。也就是说 node.js 中使用 require() 而浏览器端使用 import

参考链接

https://nodejs.dev/learn/introduction-to-nodejs

标签:无

你的评论