node.js 程序_如何不使用外部程序包创建Node.js Web应用程序

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:

我们将构建一个应用程序,它将:

  1. Import necessary modules

    导入必要的模块

  2. Create a Server instance

    创建一个服务器实例

  3. Attach listeners to the request event of the server object

    将侦听器附加到服务器对象的request事件

  4. Parse request body and headers

    解析请求正文标头

  5. Sending Response to the client.

    客户端发送响应

  6. Handle error events at request and response streams.

    处理请求和响应流中的错误事件

But, here is the catch ;)

但是,这是要抓住的地方;)

We will do all of it from scratch with just

我们将仅从头开始做所有这一切

  1. a terminal, and

    一个终端,以及

  2. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值