node.js 程序
by Abhinav Pandey
通过Abhinav Pandey
如何不使用外部程序包创建Node.js Web应用程序 (How to create a Node.js web app using no external packages)
没有框架,没有NPM,没有Package.json,没有多余的装饰 (No frameworks, no NPM, no Package.json, no frills)
In this post, we will dive deep inside Node.js fundamentals by creating a Node.js web app without any external packages. We will cover core concepts like streams, events, exceptions, HTTP etc.
在本文中,我们将通过创建一个没有任何外部包的Node.js Web应用程序来深入探究Node.js的基础知识 。 我们将涵盖流,事件,异常,HTTP等核心概念。
Currently, whenever we say we are going to implement a service in Node.js, most of the time we are going to use Express or other 3rd party libraries to implement our functionality. And I am not going to say there is any harm in doing that. These libraries provide necessary abstraction over redundant concepts which make us efficient.
当前,每当我们说要在Node.js中实现服务时,大多数时候我们将使用Express或其他第三方库来实现我们的功能。 而且我不会说这样做有任何危害。 这些库提供了使我们高效的冗余概念的必要抽象。
But with greater abstraction, the low-level logic of your program is hidden from you. As a result, we’re not able to develop a clear picture of how our business logic interacts with Node.js.
但是有了更大的抽象,您的程序的底层逻辑就被隐藏了。 结果,我们无法清楚地了解我们的业务逻辑如何与Node.js交互。
But as Ryan Dahl, the creator of Node.js said:
但是正如Node.js的创建者Ryan Dahl所说:
You can never understand everything. But, you should push yourself to understand the system.
您永远无法理解一切。 但是,您应该推动自己去理解系统。
We will push ourselves to form this clear picture in entirety.
我们将全力以赴形成这张清晰的图画。
So, let’s build a raw HTTP Node.js application with no Framework, no NPM, and no Package.json.
因此,让我们构建一个没有框架,没有NPM,也没有Package.json的原始HTTP Node.js应用程序。
We will build an app that will:
我们将构建一个应用程序,它将:
Import necessary modules
导入必要的模块
Create a Server instance
创建一个服务器实例
Attach listeners to the
request
event of the server object将侦听器附加到服务器对象的
request
事件Parse request body and headers
解析请求正文和标头
Sending Response to the client.
向客户端发送响应 。
Handle error events at request and response streams.
处理请求和响应流中的错误事件 。
But, here is the catch ;)
但是,这是要抓住的地方;)
We will do all of it from scratch with just
我们将仅从头开始做所有这一切
a terminal, and
一个终端,以及
an editor.
编辑。
Yes!! We will use nobody else’s framework, nobody else’s libraries just raw JavaScript and core Node.js runtime.
是!! 我们将使用其他人的框架 , 其他人的库仅使用原始JavaScript和核心Node.js运行时 。
Let’s Begin!
让我们开始!
Before creating an HTTP server, let’s clear up the necessary concept of an HTTP module in Node.js.
在创建HTTP服务器之前,让我们在Node.js中清除HTTP模块的必要概念。
What is HTTP?
什么是HTTP?
http
in Node.js is an inbuilt module that allows client-server communication via HTTP protocol. This module provides an interface to create either an HTTP client or HTTP server that can communicate with other HTTP servers or clients.
Node.js中的http
是一个内置模块,允许通过HTTP协议进行客户端-服务器通信。 该模块提供了一个接口,以创建可以与其他HTTP服务器或客户端进行通信的HTTP客户端或HTTP服务器。
And to make this communication space efficient, an http
module provides streaming of data using stream interface. And since stream passes data in chunks, that means Node.js never buffers the entire request or response at once in the memory. We will come to streams soon.
而为了让这个通信节省空间,一个http
模块提供的流 数据使用流接口。 而且由于流将数据分块传递,这意味着Node.js永远不会在内存中一次缓冲整个请求或响应。 我们将很快来到流 。
So for our app, we will use this http
interface to create an HTTP server that will listen to a particular port and give data back to the user.
因此,对于我们的应用程序,我们将使用此http
接口创建一个HTTP服务器,该服务器将侦听特定端口并将数据返回给用户。
导入HTTP模块 (Importing the HTTP module)
To use either the http
server or client you must require http
module.
要使用http
服务器或客户端,您必须使用http
模块。
var http = require(“http”);
Now let us see how the above line actually works:
现在让我们看看上面的行实际上是如何工作的:
For loading an instance of a particular module in our runtime, Node.js pr