javascript 没有任何 Web 框架的原始 Node.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28493574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:04:14  来源:igfitidea点击:

Raw Node.js without any web framework

javascriptnode.js

提问by Core_dumped

I'm trying to learn node, I saw lot of material (as suggested in that famous SO question), the problem is that all the books I saw, or tutorials either use a web framework like express or simply limit themselves to explain what node is and go no deeper than explaining how to create a very basic http server which listens for requests on some port.
So I'm really wondering is there anybody who uses node w/o a web framework? If so they must have learned it somewhere, so could you please suggest where can I learn it?
I know that this is very low level, but I don't mind, I have already a familiarity on how to create servers in C.
I'd really like to understand how we really serve static content with node (customly organized in folders) and how we actually introduce logic into our html (I looked all this up but found only results on how to do it using Express, where logic is introduced by something like <% //code %>, but can this be done in pure node?).

The question similar to mine which has been already asked here does not really have an answer. The best answer just suggests to read the official nodejs documentation which is like saying:
there you have a dictionary with millions of words in Latin, good luck in learning it.

So can you suggest some well documented, robust book/ tutorial where is shown how to create a real world website using raw node?
If not, I guess I'll have to stick to php + Apache and try to optimize those to scale.

我正在尝试学习 node,我看到了很多材料(正如那个著名的 SO 问题中所建议的那样),问题是我看到的所有书籍或教程要么使用像 express 这样的网络框架,要么只是限制自己解释什么节点只是解释如何创建一个非常基本的 http 服务器来侦听某个端口上的请求。
所以我真的想知道有没有人使用 node w/oa web 框架?如果是这样,他们一定是在某个地方学到的,所以请您建议我在哪里可以学到它?
我知道这是非常低的水平,但我不介意,我已经熟悉如何在 C 中创建服务器。
我真的很想了解我们如何真正通过节点(自定义组织在文件夹中)提供静态内容以及我们如何实际将逻辑引入我们的 html(我查看了所有这些,但只找到了有关如何使用 Express 进行操作的结果,其中逻辑由<% //code %> 之类的东西引入,但这可以在纯节点中完成吗?)。

与我已经在这里提出的问题类似的问题并没有真正的答案。最好的答案只是建议阅读官方的 nodejs 文档,这就像在说:
你有一本包含数百万拉丁语单词的字典,祝你学习好运。

那么你能推荐一些有据可查的、强大的书籍/教程,其中展示了如何使用原始节点创建一个真实世界的网站?
如果没有,我想我将不得不坚持使用 php + Apache 并尝试优化它们以进行扩展。

采纳答案by jfriend00

Node.js by itself is a Javascript execution engine (based on V8) that runs on many different platforms and comes with a standard library. It's somewhat analogous to any other interpreted language with its standard library (such as Python or PHP). It would not be accurate to describe plain node.js as a "web platform" by itself. It has the core tools where one can make a web platform out of it, but it can also be used for all sorts of other types of uses that have nothing to do with being a web platform. For example, I've built some command line build tools out of it for doing various forms of text processing (a use that does no networking of any kind).

Node.js 本身是一个 Javascript 执行引擎(基于 V8),可以在许多不同的平台上运行并带有一个标准库。它有点类似于任何其他具有标准库的解释性语言(例如 Python 或 PHP)。将普通的 node.js 本身描述为“网络平台”是不准确的。它具有核心工具,可以从中构建网络平台,但它也可用于与网络平台无关的各种其他类型的用途。例如,我用它构建了一些命令行构建工具,用于进行各种形式的文本处理(不进行任何类型的网络连接)。

So I guess "raw" node just means solving whatever problem you want to solve without building on top of 3rd party libraries (beyond the standard library that node.js comes with). Personally, I'm not sure why you really want to do that. One huge advantage of node.js development is the whole NPM eco-system where there are thousands of pre-built, free and open source modules that solve thousands of problems. Some are a few functions, but still useful and others are whole APIs that solve rich sets of problems. The beauty of NPM and this eco-system is that with one simple command you can add any one of these modules to your project and with a couple lines of code, you can be using it in your project. I'd consider it silly to avoid this advantage.

所以我猜“原始”节点只是意味着解决你想要解决的任何问题,而无需建立在 3rd 方库之上(超出 node.js 附带的标准库)。就我个人而言,我不确定你为什么真的想要这样做。node.js 开发的一大优势是整个 NPM 生态系统,其中有数以千计的预构建、免费和开源模块可以解决数以千计的问题。有些是一些功能,但仍然有用,而另一些则是解决丰富问题集的完整 API。NPM 和这个生态系统的美妙之处在于,您可以通过一个简单的命令将这些模块中的任何一个添加到您的项目中,并且只需几行代码,您就可以在您的项目中使用它。我认为避免这种优势很愚蠢。

So learning raw node.js means learning Javascript, learning the tools often used for node.js development (debugger, NPM, console, etc...) and learning the standard library that comes with node.js. Few people ever want to sit down and actually learn every function in the standard library. Usually what people do is take a good long look at all the modules available in the standard library, page through each one to get a sense of what kinds of things they have in them and then find yourself something you want to build and start building it. As you are forced to find things and figure out how they work from the documentation or from Google searches or from studying other node.js code you find, you will learn how portions of the standard library works and what it does. If you are doing I/O (file, network, etc...), you will quickly encounter lots of asynchronous APIs in the standard library and you will generally want or need to become proficient at handling async operations (which is really just learning async Javascript), but will likely be important in a node.js project.

所以学习原生 node.js 就是学习 Javascript,学习 node.js 开发常用的工具(调试器、NPM、控制台等),学习 node.js 自带的标准库。很少有人愿意坐下来真正学习标准库中的每一个函数。通常人们所做的是仔细查看标准库中所有可用的模块,逐页浏览每个模块以了解其中包含哪些类型的东西,然后找到自己想要构建的东西并开始构建它. 当您被迫从文档或 Google 搜索或研究您找到的其他 node.js 代码中查找内容并弄清楚它们是如何工作的时,您将了解标准库的部分内容是如何工作的以及它的作用。如果您正在执行 I/O(文件、网络等...),

If you really want to "study" the standard library by itself, then both Amazon and Google have long lists of resources you can page through to see which ones seem to approach things the way you want to. Asking for us to find such a resource for you is considered "off topic" here on StackOverflow so I will leave you to go consult those lists and decide what looks interesting. I myself knew client-side Javascript and picked up node.js by just reading some web resources and then working on my own projects. Eventually, I built a node.js system that runs on a Raspberry Pi and sits in my attic monitoring temperature probes, switching attic fans based on temperature differences and offering a web interface for controlling, configuring and reporting on everything that is going on. It's half a web app and half a stand-alone temperature controller.

如果你真的想自己“研究”标准库,那么亚马逊和谷歌都有很长的资源列表,你可以翻阅,看看哪些似乎以你想要的方式处理事情。要求我们为您找到这样的资源在 StackOverflow 上被认为是“题外话”,所以我会让您去查阅这些列表并决定哪些看起来很有趣。我自己知道客户端 Javascript 并通过阅读一些 Web 资源然后开始我自己的项目来学习 node.js。最终,我构建了一个在 Raspberry Pi 上运行的 node.js 系统,它位于我的阁楼监控温度探测器,根据温差切换阁楼风扇,并提供一个 Web 界面来控制、配置和报告正在发生的一切。它'

Unfortunately, the documentation for the node.js standard library is not richly descriptive (I'm being kind there). It's technically accurate, but often doesn't answer many of the common questions anyone wanting to use an API would quickly have. It appears to assume that you already know a lot of the Unix C standard library as it has many similar functions (particularly for file access). Plus, one of the documentation drawbacks of an object hierarchy (where things inherit from other things) is that it can be hard to put together in one place everything that a given object does. Instead you have to mentally collect and understand what the base objects do and then try to figure out how that fits in to what the root object is doing. This isn't a challenge that only node.js faces, lots of object oriented systems have this documentation challenge (it used to drive me nuts with YUI).

不幸的是,node.js 标准库的文档描述不够丰富(我很友好)。它在技术上是准确的,但通常不能回答任何想要使用 API 的人很快就会遇到的许多常见问题。它似乎假设您已经了解很多 Unix C 标准库,因为它具有许多类似的功能(特别是对于文件访问)。另外,对象层次结构(事物从其他事物继承)的文档缺点之一是很难将给定对象所做的一切放在一个地方。相反,您必须在头脑中收集并理解基础对象的作用,然后尝试弄清楚它如何适应根对象的作用。这不是只有 node.js 面临的挑战,

What I quickly found is that I really needed to be able to find the relevant library source code for any standard library function so I could look at how the function was actually written in order to answer my questions. This is such a huge advantage of both open source and how searchable GitHub is (all the standard library source is on GitHub).

我很快发现,我真的需要能够找到任何标准库函数的相关库源代码,以便我可以查看函数的实际编写方式,以便回答我的问题。这是开源和 GitHub 可搜索性的巨大优势(所有标准库源都在 GitHub 上)。

So, page through all the modules in the node.js standard library to familiarize yourself with what is there and where you'd find it. Then find yourself an app you want to build and build it. If you're building a web app, I can think of no reason why you would ever want to do that without using a framework someone else has already built for you (I use Express). There's just no reason to reinvent everything yourself. If you want to make sure you understand the HTTP module before using Express, then build yourself a simple web server using only the HTTP module that serves two static files and use the HTTP module going the other way to request a couple web pages from other servers. Then, start using Express to build your own web app.

因此,请浏览 node.js 标准库中的所有模块,以熟悉其中的内容以及可以找到它的位置。然后找到自己想要构建的应用程序并构建它。如果您正在构建一个网络应用程序,我想不出为什么您会想要在不使用其他人已经为您构建的框架的情况下这样做(我使用 Express)。没有理由自己重新发明一切。如果您想确保在使用 Express 之前了解 HTTP 模块,那么仅使用提供两个静态文件的 HTTP 模块为自己构建一个简单的 Web 服务器,并使用 HTTP 模块以另一种方式从其他服务器请求几个网页. 然后,开始使用 Express 构建您自己的 Web 应用程序。

As for some of your more specific questions:

至于你的一些更具体的问题:

I'd really like to understand how we really serve static content with node (customly organized in folders)

我真的很想了解我们如何使用节点(自定义组织在文件夹中)真正提供静态内容

Some of the answers to this questionshow you a basic node-only web server that serves up static content from a file system. More examples and explanation here.

这个问题的一些答案向您展示了一个基本的仅节点 Web 服务器,它提供来自文件系统的静态内容。更多例子和解释在这里

how we actually introduce logic into our html (I looked all this up but found only results on how to do it using Express, where logic is introduced by something like <% //code %>, but can this be done in pure node?).

我们实际上是如何将逻辑引入到我们的 html 中的(我查了所有这些,但只找到了如何使用 Express 来实现的结果,其中逻辑是由诸如 <% //code %> 之类的东西引入的,但这可以在纯节点中完成吗? )。

Introducing "logic" into your HTML from scratch means that you first build a system for serving static web pages and then you add to that a system for parsing through the web pages (on the server) to find directives in them that mean your server should do something to modify or add to the HTML before sending it to the client. There are many different ways of doing this which is why there are probably 50 different systems out there already for doing this. A search for node.js template systems will find you a list. Again, I have no idea why you would want to build one of these from scratch. It is a bit of a research project to figure out which of the zillions out there matches your desires the best (I'm using Handlebars, a derivative of Mustache myself), but that should be way, way less work than building your own system from scratch. And, even if you want some grand capabilities that can't be easily accomplished with a stock system, you may as well start with a stock system and extend it.

从头开始将“逻辑”引入您的 HTML 意味着您首先构建一个系统来提供静态网页,然后添加一个系统来解析网页(在服务器上)以查找其中的指令,这意味着您的服务器应该在将 HTML 发送给客户端之前,对其进行一些修改或添加。有许多不同的方法可以做到这一点,这就是为什么已经有 50 种不同的系统可以做到这一点。搜索 node.js 模板系统会找到一个列表。同样,我不知道您为什么要从头开始构建其中之一。这是一个研究项目,以确定哪些最符合您的愿望(我正在使用 Handlebars,我自己的 Mustache 的衍生物),但这应该是这样的,比从头开始构建自己的系统要少得多。而且,即使您想要一些无法通过库存系统轻松实现的强大功能,您也可以从库存系统开始并对其进行扩展。

回答by Yuri Zarubin

What you're asking for seems to be two different things, you want to do raw Node, but at the same time know how people use it in the real world. Pretty much everyone uses at the very least, Express (or Koa if you're on the bleeding edge) as their web framework. They provide the bare bones to create a robust web server. Then, for your actual front end, you'd use either AngularJS or ReactJS, you wouldn't do any rendering server side template rendering (e.g. no <% %> code blocks). If you choose to go the React route, you'll need additional libraries like BaconJS to assist with the glue, as React is just the view layer, whereas Angular is an all encompassing MVC framework.

你所要求的似乎是两件不同的事情,你想做原始 Node,但同时知道人们如何在现实世界中使用它。几乎每个人都至少使用 Express(或 Koa,如果您处于前沿)作为他们的 Web 框架。它们提供了创建强大的 Web 服务器的基础。然后,对于您的实际前端,您将使用 AngularJS 或 ReactJS,您不会进行任何渲染服务器端模板渲染(例如,没有 <% %> 代码块)。如果你选择走 React 路线,你将需要像 BaconJS 这样的额外库来帮助粘合,因为 React 只是视图层,而 Angular 是一个包罗万象的 MVC 框架。

One of the best ways to learn, is to look at the MEAN stack yeoman generator https://github.com/DaftMonk/generator-angular-fullstack. Use the generator to make an app, and read its source to see how it is structured, and just start hacking away at it to get to do what you want.

最好的学习方法之一是查看 MEAN stack yeoman generator https://github.com/DaftMonk/generator-angular-fullstack。使用生成器制作一个应用程序,并阅读它的源代码以了解它的结构,然后开始破解它来做你想做的事。