node.js简介
Node.js is a runtime environment for JavaScript that runs on the server.
Node.js是在服务器上运行的JavaScript的运行时环境 。
Node.js is open source, cross-platform, and since its introduction in 2009, it got hugely popular and now plays a significant role in the web development scene. If GitHub stars are one popularity indication factor, having 58000+ stars means being very popular.
Node.js是开放源代码,跨平台的,自2009年问世以来,它非常受欢迎,现在在Web开发领域中发挥着重要作用。 如果GitHub星级是一个受欢迎程度的指标,那么拥有58000多个星级意味着非常受欢迎。
Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser. Node.js is able to leverage the work of the engineers that made (and will continue to make) the Chrome JavaScript runtime blazing fast, and this allows Node.js to benefit from the huge performance improvements and the Just-In-Time compilation that V8 performs. Thanks to this, JavaScript code running in Node.js can become very performant.
Node.js在浏览器外部运行V8 JavaScript引擎(Google Chrome的核心)。 Node.js能够利用使(并将继续制造)Chrome JavaScript运行时Swift发展的工程师的工作,这使Node.js能够从巨大的性能改进和即时编译中受益。 V8执行。 因此,在Node.js中运行JavaScript代码可以变得非常出色。
A Node.js app is run by a single process, without creating a new thread for every request. Node provides a set of asynchronous I/O primitives in its standard library that will prevent JavaScript code from blocking and generally, libraries in Node.js are written using non-blocking paradigms, making a blocking behavior an exception rather than the normal.
Node.js应用程序由单个进程运行,无需为每个请求创建新线程。 Node在其标准库中提供了一组异步I / O原语,这将防止JavaScript代码被阻塞,并且通常,Node.js中的库是使用非阻塞范式编写的,从而使阻塞行为成为异常而不是常规行为。
When Node.js needs to perform an I/O operation, like reading from the network, access a database or the filesystem, instead of blocking the thread Node.js will resume the operations when the response comes back, instead of wasting CPU cycles waiting.
当Node.js需要执行I / O操作(例如从网络读取,访问数据库或文件系统)而不是阻塞线程时,Node.js将在响应返回时恢复操作,而不是浪费CPU等待周期。
This allows Node.js to handle thousands of concurrent connections with a single server without introducing the burden of managing threads concurrency, which would be a major source of bugs.
这使Node.js可以在一台服务器上处理数千个并发连接,而不会带来管理线程并发的负担,这将是漏洞的主要来源。
Node.js has a unique advantage because millions of frontend developers that write JavaScript for the browser are now able to run the server-side code and frontend-side code without the need to learn a completely different language.
Node.js具有独特的优势,因为数百万为浏览器编写JavaScript的前端开发人员现在可以运行服务器端代码和前端代码,而无需学习完全不同的语言。
In Node.js the new ECMAScript standards can be used without problems, as you don’t have to wait for all your users to update their browsers - you are in charge of deciding which ECMAScript version to use by changing the Node.js version, and you can also enable specific experimental features by running Node with flags.
在Node.js中,可以毫无问题地使用新的ECMAScript标准,因为您不必等待所有用户更新其浏览器-您可以通过更改Node.js版本来决定使用哪个ECMAScript版本,并且您还可以通过运行带有标志的Node来启用特定的实验功能。
npm
with its simple structure helped the ecosystem of node.js proliferate and now the npm registry hosts almost 500.000 open source packages you can freely use.
npm
具有简单的结构,可帮助node.js生态系统Swift发展,现在npm注册表托管了近500.000个您可以自由使用的开源软件包。
The most common example Hello World of Node.js is a web server:
最常见的Node.js的Hello World示例是Web服务器:
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}/`)
})
To run this snippet, save it as a server.js
file and run node server.js
in your terminal.
要运行此代码段,请将其另存为server.js
文件,然后在终端中运行node server.js
。
This code first includes the Node.js http
module.
此代码首先包含Node.js http
模块 。
Node.js has an amazing standard library, including a first-class support for networking.
Node.js具有出色的标准库 ,包括对网络的一流支持。
The createServer()
method of http
creates a new HTTP server and returns it.
http
的createServer()
方法创建一个新的HTTP服务器并返回它。
The server is set to listen on the specified port and hostname. When the server is ready, the callback function is called, in this case informing us that the server is running.
服务器设置为侦听指定的端口和主机名。 服务器准备好后,将调用回调函数,在这种情况下,通知我们服务器正在运行。
Whenever a new request is received, the request
event is called, providing two objects: a request (an http.IncomingMessage
object) and a response (an http.ServerResponse
object).
每当接收到新请求时,都会调用request
事件 ,并提供两个对象:一个请求(一个http.IncomingMessage
对象)和一个响应(一个http.ServerResponse
对象)。
Those 2 objects are essential to handle the HTTP call.
这两个对象对于处理HTTP调用至关重要。
The first provides the request details. In this simple example, this is not used, but you could access the request headers and request data.
第一个提供请求详细信息。 在这个简单的示例中,没有使用它,但是您可以访问请求标头和请求数据。
The second is used to return data to the caller.
第二个用于将数据返回给调用方。
In this case with
在这种情况下
res.statusCode = 200
we set the statusCode property to 200, to indicate a successful response.
我们将statusCode属性设置为200,以指示响应成功。
We set the Content-Type header:
我们设置Content-Type标头:
res.setHeader('Content-Type', 'text/plain')
and we end close the response, adding the content as an argument to end()
:
然后结束响应,将内容作为参数添加到end()
:
res.end('Hello World\n')
Node.js is a low-level platform, and to make things easier and more interesting for developers thousands of libraries were built upon Node.js.
Node.js是一个低级平台,为了使开发人员更轻松,更有趣,在Node.js上构建了数千个库。
Many of those established over time as popular options. Here is a non-comprehensive list of the ones I consider very relevant and worth learning:
随着时间的流逝,其中许多已成为受欢迎的选择。 以下是我认为非常相关且值得学习的内容的不完整列表:
Express, 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.
Express ,这是创建Web服务器的最简单但功能最强大的方法之一。 其极简主义的方法不受限制,专注于服务器的核心功能,是其成功的关键。
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.
Meteor是一个功能强大的全栈框架,它以同构方法为您提供支持,以JavaScript构建应用程序,在客户端和服务器上共享代码。 曾经提供所有功能的现成工具,现在可以与前端库React, Vue和Angular集成。 也可以用于创建移动应用。
koa, 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.
由Express背后的同一团队构建的koa旨在在多年的知识基础上进一步简化和缩小。 新项目的产生是出于创建不兼容的变更而又不破坏现有社区的需要。
Next.js, a framework to render server-side rendered React applications.
Micro, a very lightweight server to create asynchronous HTTP microservices.
Micro ,这是一种非常轻量级的服务器,用于创建异步HTTP微服务。
Socket.io, a real-time communication engine to build network applications.
Socket.io ,用于构建网络应用程序的实时通信引擎。
node.js简介