Javascript 使用 Node.js 构建网站的最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11311672/
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-08-24 05:24:28  来源:igfitidea点击:

Best practices of building a website using Node.js

javascriptnode.jsmodel-view-controller

提问by Benjamin Gruenbaum

I was wondering how one would go about developing a website from scratch with Node.js. I understand how I could possiblydo it, but I am interested in the bestdesign practice.

我想知道如何使用 Node.js 从头开始​​开发网站。我知道我怎么可能做到这一点,但我对最佳设计实践感兴趣。

I need this theoretical website to:

我需要这个理论网站来:

  1. Do a lot of AJAX
  2. Be very straightforward
  3. Be relatively small
  4. Connect to... let's say a MySQL server
  1. 做了很多 AJAX
  2. 直截了当
  3. 相对较小
  4. 连接到...让我们说一个 MySQL 服务器

In PHP, building a pretty small website was very straightforward - I set up PHP on Apache and a MySQL server and then do something like:

在 PHP 中,构建一个非常小的网站非常简单 - 我在 Apache 和 MySQL 服务器上设置 PHP,然后执行以下操作:

  • includes/db/which has connect.php for connecting to the db, a file with common db related functions and so on
  • includes/layout/which had stuff like footer.php, header.php, and other layout related stuff
  • includes/users/to handle user related actions
  • includes/db/其中有connect.php用于连接数据库,一个具有常用数据库相关功能的文件等等
  • includes/layout/其中包含footer.php、header.php和其他与布局相关的内容
  • includes/users/处理用户相关的操作

Then PHP just let you build pages and include these files together to form a website - I could go something like:

然后 PHP 只是让您构建页面并将这些文件包含在一起形成一个网站 - 我可以这样做:

<?php
   require_once('inclues/users/user_session.php');
   require_once('inclues/db/connect.php');
   require_once('inclues/design/header.php')
?>

// Other php or html or related content relating to the page

<?php
   require_once('inclues/.../footer.php');
?>

I was wondering what might be similar in Node.js - I am looking for a way to accomplish this which is as simple, fastand straightforwardas possible.

我想知道 Node.js 中可能有什么相似之处 - 我正在寻找一种尽可能简单快速直接的方法来实现这一点。

If the answer is not simple, I would love a book recommendation, I don't mind reading.

如果答案不简单,我会喜欢一本书推荐,我不介意阅读。

I love event based programming, I really love JavaScript's abilities and I'm really excited about Node.js. I want to learn how to develop this sort of stuff with it the right way from the start.

我喜欢基于事件的编程,我真的很喜欢 JavaScript 的能力,我对 Node.js 真的很兴奋。我想从一开始就学习如何用它以正确的方式开发这类东西。

回答by Rem.co

To start with the bad news: As Node.jsis a pretty young technique, I think you'll find that the proces of creating a full fledged website and maintaining/operating it will be very different than what you're currently used to.

先从坏消息说起:由于Node.js是一项非常年轻的技术,我认为您会发现创建一个完整的网站和维护/运营它的过程将与您目前习惯的非常不同。

Josh3736adds: Once you figure out how Node.js and its various packages (Connect, Express) work, I found that you can develop new sites very quickly.

Josh3736补充道:一旦你弄清楚 Node.js 及其各种包(Connect、Express)是如何工作的,我发现你可以非常快速地开发新站点。

The rough edges that currently exist in Node.js, combined with the fast pace of its development and all modules involved can complicate things though, and make things less simple, fast and straightforward than you'd like.

Node.js 当前存在的粗糙边缘,再加上其快速的开发速度和所有涉及的模块,可能会使事情复杂化,并使事情变得不像您想要的那样简单、快速和直接。

Having that out of the way, here's the good news:

有了这个,这里是个好消息:

The Node Package Manager, NPMhas a lot of good tools and frameworks to expand Node.js's bare bones functionality, making it suitable to create a webserver.

节点包管理器,NPM有很多很好的工具和框架,扩大Node.js的裸露的骨头的功能,使其适合于创建一个Web服务器。

Most notably would be the Express Frameworkwhich contains almost everything you need to run a webserver (including cookies, sessions and path routing). Additionally Express supports partials, which take care of your header and footer includes.

最值得注意的是Express 框架,它包含运行网络服务器所需的几乎所有内容(包括 cookie、会话和路径路由)。此外,Express 支持部分,它负责您的页眉和页脚包含。

Express is built on top of Sencha's Connect. Cookies and sessions are actually powered by Connect. Express is what simplifies your routing and handles views/partials. So if you don't need all bells and whistles that come with Express you could just go for Connect instead.

Express 建立在Sencha 的 Connect之上。Cookie 和会话实际上由 Connect 提供支持。Express 可以简化您的路由并处理视图/部分。因此,如果您不需要 Express 附带的所有花里胡哨的功能,您可以选择 Connect。

If you like to use templates for these partials, the Jade Template Enginecan speed things up for you. Though Josh3736points out that Jade is slow and whitespace-significant. A more complete overview can be found here, which includes his favourite, doT. (I personally use Node.js for socket.iobased applications only, so he's a better source than me when it comes to templating).

如果您喜欢为这些部分使用模板,Jade 模板引擎可以为您加快速度。尽管Josh3736指出 Jade 速度慢且空格重要。可以在此处找到更完整的概述,其中包括他最喜欢的doT。(我个人socket.io仅将 Node.js 用于基于应用程序,因此在模板方面,他是比我更好的来源)。

You canconnect to MySQL from Node.js using the db-mysqlmodule, but if you don't need that because you're accessing data connected to an already present system, I'd advise to use a more... 'modern' approach, which is to use a NoSQLdatabase as most Node.js projects seem to do. MongoDBvia Mongooseis the popular way to go.

可以使用db-mysql模块从 Node.js 连接到 MySQL ,但如果您不需要它,因为您正在访问连接到现有系统的数据,我建议使用更多...'现代' 方法,即像大多数 Node.js 项目一样使用NoSQL数据库。MongoDB通过Mongoose是流行的方式。

Or if it's just storing objects you're interested in, just go for Redisinstead (which you're probably going to need at some point anyway).

或者,如果它只是存储您感兴趣的对象,请改为使用Redis(无论如何您可能会在某个时候需要它)。

Once your website is complete, you'll have to deploy it and make sure it keeps running. There are many ways to do so, like using built-in cluster support or use the more feature-friendly forevernpm module. See this SO question of mine for more information.

网站完成后,您必须部署它并确保它继续运行。有很多方法可以做到这一点,比如使用内置的集群支持或使用功能更友好的forevernpm 模块。有关更多信息,请参阅我的这个 SO 问题。

Conclusion:

结论

What I'm trying to get at is this:

我想要得到的是:

Asking what the best practice for building a website in Node.js is, is about the same as asking what the best way to build a website in PHP is: 100 developers will give you 100 different answers.

询问在 Node.js 中构建网站的最佳实践是什么,与询问在 PHP 中构建网站的最佳方法是什么大致相同:100 名开发人员会给你 100 个不同的答案。

NPM is blessed with a variety of excellent frameworks that greatly simplify a lot of tasks involved, but it's all based on preference which one is the way to go really.

NPM 拥有各种优秀的框架,这些框架极大地简化了所涉及的许多任务,但这一切都取决于偏好,哪一种才是真正要走的路。

As I've said, Node.js is still a pretty young technique, so none of the frameworks or additional tools have emerged as 'defacto standard' yet; for most things you're trying to do there are probably various alternatives, and expect your code to break when using most of them during updates, because development of Node.js itself and most modules is fast paced. You'll have to keep up.

正如我所说,Node.js 仍然是一项非常年轻的技术,因此还没有任何框架或其他工具成为“事实上的标准”;对于您尝试做的大多数事情,可能有多种替代方案,并且在更新期间使用其中大部分时,预计您的代码会中断,因为 Node.js 本身和大多数模块的开发节奏很快。你必须跟上。

Putting it all together:

把它们放在一起

As I've said, my main production use for Node.js is to be able to use socket.io, so I don't have any good production examples present (And as I'm about to leave on a well-deserved vacation I don't have the time to put one together either). There are some good examples though:

正如我所说的,我对 Node.js 的主要生产用途是能够使用socket.io,所以我没有任何好的生产示例(因为我即将离开一个当之无愧的假期,我没有”也没有时间把它们放在一起)。不过也有一些很好的例子:

Again, the way to go (and subsequently the example to follow) depends greatly on your ultimate goals and the techniques chosen, but luckily there are plenty of resources available for all of the choices available. Most modules use well documented GitHub repositories and include examples in combination with the most popular modules (See the /examples/dir that seems to be present in most repositories).

同样,要走的路(以及随后要遵循的示例)在很大程度上取决于您的最终目标和所选择的技术,但幸运的是,所有可用的选择都有大量可用资源。大多数模块使用记录良好的 GitHub 存储库,并包含与最流行的模块组合的示例(请参阅/examples/大多数存储库中似乎存在的目录)。

Good luck! (And thanks to Josh3736for rectifying my errors.)

祝你好运!(并感谢Josh3736纠正我的错误。)