原始节点。没有任何web框架的js

时间:2021-05-30 18:27:57

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.

我想学习节点,我看到很多的材料(如在著名的问题),问题是所有的书我看到,或教程使用web框架表达或简单地限制自己解释什么是节点,再比解释如何创建一个非常基本的http服务器端口侦听请求。我想知道有没有人使用node w/o一个web框架?如果是这样的话,他们一定是在什么地方学的,你能告诉我在哪里可以学吗?我知道这是非常低的水平,但我不介意,我已经熟悉如何创建服务器c .我非常想了解我们服务静态内容与节点(自定义文件夹组织)和我们如何介绍逻辑到我们的html(我看着这一切,但只发现结果如何使用表达,逻辑是引入了< % % > / /代码,但这是在纯节点完成吗?)。这里已经问过的与我类似的问题并没有真正的答案。最好的答案就是阅读官方的nodejs文档,就像在说:你有一本字典,里面有数百万个拉丁语单词,祝你好运。那么,你能给我推荐一些有良好文档记录的、健壮的书籍/教程吗?如果没有,我想我必须坚持php + Apache,并尝试优化它们的规模。

2 个解决方案

#1


11  

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

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

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.

所以我猜“原始”节点仅仅意味着解决任何您想要解决的问题,而不需要在第三方库之上构建(超越该节点的标准库)。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.

所以学习原始节点。js意味着学习Javascript,学习节点常用的工具。js开发(调试器、NPM、控制台等等)和学习node.js附带的标准库。很少有人愿意坐下来真正地学习标准库中的每个函数。通常,人们所做的就是仔细研究标准库中所有可用的模块,浏览每一个模块,了解它们里面有什么东西,然后找到你想要构建的东西并开始构建它。当您*从文档、谷歌搜索或研究其他节点中查找并找出它们的工作方式时。找到的js代码,您将了解标准库的部分是如何工作的,以及它是如何工作的。如果您正在执行I/O(文件、网络等),您将很快在标准库中遇到许多异步api,并且您通常希望或需要熟练地处理异步操作(实际上就是学习异步Javascript),但这在节点中可能很重要。js的项目。

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

如果您真的想要“研究”标准库本身,那么Amazon和谷歌都有长长的资源列表,您可以通过页面查看哪些资源似乎可以按照您希望的方式进行处理。在*网站上,要求我们为你找到这样的资源被认为是“离题”,所以我让你去查阅这些列表,然后决定什么看起来有趣。我自己知道客户端Javascript并选择了node。通过阅读一些web资源,然后进行我自己的项目。最后,我构建了一个节点。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).

不幸的是,节点的文档。js标准库的描述不是很丰富(我在那里很好)。它在技术上是准确的,但通常不能回答任何想要使用API的人会很快遇到的许多常见问题。它似乎假定您已经了解了许多Unix C标准库,因为它有许多类似的功能(特别是对于文件访问)。此外,对象层次结构(从其他事物继承东西的地方)的一个文档缺陷是,很难将给定对象所做的一切放在一个地方。相反,您必须在头脑中收集和理解基对象的功能,然后尝试找出它如何与根对象的功能相适应。这并不是唯一的挑战。js faces,很多面向对象的系统都有这样的文档挑战(它曾经让我和YUI一起抓狂)。

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.

所以,页面通过节点中的所有模块。js标准库,使您熟悉那里的内容和您可以在哪里找到它。然后给自己找一个你想要构建的应用。如果您正在构建一个web应用程序,我想不出为什么您会想要使用别人已经为您构建的框架(我使用Express)。没有理由自己重新发明一切。如果您想确保在使用Express之前理解了HTTP模块,那么只需使用服务于两个静态文件的HTTP模块构建一个简单的web服务器,并使用HTTP模块从其他服务器请求几个web页面。然后,开始使用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)

我很想知道我们是如何用node来服务静态内容的(自定义地组织在文件夹中)

Some of the answers to this question show 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从头开始意味着你首先构建一个系统提供静态web页面,然后你再加上一个系统解析通过web页面(在服务器上)发现指示他们,意味着您的服务器应该做一些修改或添加到HTML之前发送给客户端。有很多不同的方法可以做到这一点,这就是为什么可能有50种不同的系统可以做到这一点。搜索节点。js模板系统将为您找到一个列表。再说一遍,我不知道你为什么要从头开始构建其中的一个。这是一个研究项目,想要弄清楚你的欲望和你的欲望有多大的关系(我用的是把手,我自己的一种小胡子),但这应该是一种方法,比从头开始构建你自己的系统要少得多。而且,即使您想要一些强大的功能,而这些功能在股票系统中是不容易实现的,您也可以从股票系统开始,并对其进行扩展。

#2


3  

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.

你所要求的似乎是两个不同的东西,你想做原始节点,但同时知道人们如何在现实世界中使用它。几乎每个人都至少使用Express(或者Koa,如果你在边缘的话)作为他们的web框架。它们为创建健壮的web服务器提供了基础。然后,对于实际的前端,您将使用AngularJS或反应物js,而不会进行任何呈现服务器端模板呈现(例如,没有<% >代码块)。如果您选择使用React路径,您将需要像BaconJS这样的附加库来辅助粘合,因为React只是视图层,而angle是一个包含所有内容的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.

最好的学习方法之一是查看平均堆栈约曼生成器https://github.com/DaftMonk/generator-angular-fullstack。使用生成器创建一个应用程序,并阅读它的源代码,看看它是如何构建的,然后开始对它进行黑客攻击,以实现您想要的目的。

#1


11  

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

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

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.

所以我猜“原始”节点仅仅意味着解决任何您想要解决的问题,而不需要在第三方库之上构建(超越该节点的标准库)。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.

所以学习原始节点。js意味着学习Javascript,学习节点常用的工具。js开发(调试器、NPM、控制台等等)和学习node.js附带的标准库。很少有人愿意坐下来真正地学习标准库中的每个函数。通常,人们所做的就是仔细研究标准库中所有可用的模块,浏览每一个模块,了解它们里面有什么东西,然后找到你想要构建的东西并开始构建它。当您*从文档、谷歌搜索或研究其他节点中查找并找出它们的工作方式时。找到的js代码,您将了解标准库的部分是如何工作的,以及它是如何工作的。如果您正在执行I/O(文件、网络等),您将很快在标准库中遇到许多异步api,并且您通常希望或需要熟练地处理异步操作(实际上就是学习异步Javascript),但这在节点中可能很重要。js的项目。

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

如果您真的想要“研究”标准库本身,那么Amazon和谷歌都有长长的资源列表,您可以通过页面查看哪些资源似乎可以按照您希望的方式进行处理。在*网站上,要求我们为你找到这样的资源被认为是“离题”,所以我让你去查阅这些列表,然后决定什么看起来有趣。我自己知道客户端Javascript并选择了node。通过阅读一些web资源,然后进行我自己的项目。最后,我构建了一个节点。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).

不幸的是,节点的文档。js标准库的描述不是很丰富(我在那里很好)。它在技术上是准确的,但通常不能回答任何想要使用API的人会很快遇到的许多常见问题。它似乎假定您已经了解了许多Unix C标准库,因为它有许多类似的功能(特别是对于文件访问)。此外,对象层次结构(从其他事物继承东西的地方)的一个文档缺陷是,很难将给定对象所做的一切放在一个地方。相反,您必须在头脑中收集和理解基对象的功能,然后尝试找出它如何与根对象的功能相适应。这并不是唯一的挑战。js faces,很多面向对象的系统都有这样的文档挑战(它曾经让我和YUI一起抓狂)。

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.

所以,页面通过节点中的所有模块。js标准库,使您熟悉那里的内容和您可以在哪里找到它。然后给自己找一个你想要构建的应用。如果您正在构建一个web应用程序,我想不出为什么您会想要使用别人已经为您构建的框架(我使用Express)。没有理由自己重新发明一切。如果您想确保在使用Express之前理解了HTTP模块,那么只需使用服务于两个静态文件的HTTP模块构建一个简单的web服务器,并使用HTTP模块从其他服务器请求几个web页面。然后,开始使用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)

我很想知道我们是如何用node来服务静态内容的(自定义地组织在文件夹中)

Some of the answers to this question show 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从头开始意味着你首先构建一个系统提供静态web页面,然后你再加上一个系统解析通过web页面(在服务器上)发现指示他们,意味着您的服务器应该做一些修改或添加到HTML之前发送给客户端。有很多不同的方法可以做到这一点,这就是为什么可能有50种不同的系统可以做到这一点。搜索节点。js模板系统将为您找到一个列表。再说一遍,我不知道你为什么要从头开始构建其中的一个。这是一个研究项目,想要弄清楚你的欲望和你的欲望有多大的关系(我用的是把手,我自己的一种小胡子),但这应该是一种方法,比从头开始构建你自己的系统要少得多。而且,即使您想要一些强大的功能,而这些功能在股票系统中是不容易实现的,您也可以从股票系统开始,并对其进行扩展。

#2


3  

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.

你所要求的似乎是两个不同的东西,你想做原始节点,但同时知道人们如何在现实世界中使用它。几乎每个人都至少使用Express(或者Koa,如果你在边缘的话)作为他们的web框架。它们为创建健壮的web服务器提供了基础。然后,对于实际的前端,您将使用AngularJS或反应物js,而不会进行任何呈现服务器端模板呈现(例如,没有<% >代码块)。如果您选择使用React路径,您将需要像BaconJS这样的附加库来辅助粘合,因为React只是视图层,而angle是一个包含所有内容的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.

最好的学习方法之一是查看平均堆栈约曼生成器https://github.com/DaftMonk/generator-angular-fullstack。使用生成器创建一个应用程序,并阅读它的源代码,看看它是如何构建的,然后开始对它进行黑客攻击,以实现您想要的目的。