简体   繁体   English

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

[英]Best practices of building a website using Node.js

I was wondering how one would go about developing a website from scratch with Node.js. 我想知道如何用Node.js从头开始开发一个网站。 I understand how I could possibly do it, but I am interested in the best design practice. 我理解我怎么可能这样做,但我对最好的设计实践很感兴趣。

I need this theoretical website to: 我需要这个理论网站:

  1. Do a lot of AJAX 做了很多AJAX
  2. Be very straightforward 非常直截了当
  3. Be relatively small 相对较小
  4. Connect to... let's say a MySQL server 连接到...让我们说一个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/db/ ,它具有用于连接到db的connect.php,具有公共db相关功能的文件,等等
  • includes/layout/ which had stuff like footer.php, header.php, and other layout related stuff includes/layout/有footer.php,header.php和其他布局相关的东西
  • includes/users/ to handle user related actions 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 , fast and straightforward as 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. 我喜欢基于事件的编程,我非常喜欢JavaScript的能力,我对Node.js感到非常兴奋。 I want to learn how to develop this sort of stuff with it the right way from the start. 我想学习如何从一开始就以正确的方式开发这种东西。

To start with the bad news: As Node.js is 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是一种非常年轻的技术,我认为你会发现创建一个完整的网站并维护/操作它的过程将与你现在习惯的非常不同。

Josh3736 adds: 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, NPM has 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的简单功能,使其适合创建一个网络服务器。

Most notably would be the Express Framework which contains almost everything you need to run a webserver (including cookies, sessions and path routing). 最值得注意的是Express Framework ,其中包含运行Web服务器所需的几乎所有内容(包括cookie,会话和路径路由)。 Additionally Express supports partials, which take care of your header and footer includes. 另外Express支持partials,它负责你的页眉和页脚包含。

Express is built on top of Sencha's Connect . Express建立在Sencha的Connect之上。 Cookies and sessions are actually powered by Connect. Cookie和会话实际上由Connect提供支持。 Express is what simplifies your routing and handles views/partials. Express可以简化您的路由并处理视图/部分。 So if you don't need all bells and whistles that come with Express you could just go for Connect instead. 因此,如果你不需要Express附带的所有铃声和口哨,你可以选择Connect。

If you like to use templates for these partials, the Jade Template Engine can speed things up for you. 如果您想为这些局部使用模板, Jade模板引擎可以为您加速。 Though Josh3736 points out that Jade is slow and whitespace-significant. 虽然Josh3736指出翡翠很慢而且空白很重要。 A more complete overview can be found here , which includes his favourite, doT . 可以在这里找到更完整的概述,其中包括他最喜欢的, doT (I personally use Node.js for socket.io based applications only, so he's a better source than me when it comes to templating). (我个人仅将Node.js用于基于socket.io的应用程序,所以在模板方面他是一个比我更好的资源)。

You can connect to MySQL from Node.js using the db-mysql module, 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 NoSQL database as most Node.js projects seem to do. 可以使用db-mysql模块从Node.js连接到MySQL,但是如果您不需要它,因为您正在访问连接到已经存在的系统的数据,我建议使用更多...'现代'方法,即使用NoSQL数据库,因为大多数Node.js项目似乎都如此。 MongoDB via Mongoose is the popular way to go. 通过Mongoose的 MongoDB是流行的方式。

Or if it's just storing objects you're interested in, just go for Redis instead (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 forever npm module. 有很多方法可以做到这一点,例如使用内置集群支持或使用功能更友好的forever npm模块。 See this SO question of mine for more information. 有关更多信息,请参阅我的这个问题。

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; 正如我所说,Node.js仍然是一种非常年轻的技术,因此没有任何框架或其他工具已经成为“事实上的标准”; 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. 对于你正在尝试做的大多数事情,可能有各种各样的选择,并期望你的代码在更新期间使用大部分代码时会中断,因为Node.js本身和大多数模块的开发都是快节奏的。 You'll have to keep up. 你必须跟上。

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). 正如我所说,我对Node.js的主要生产用途是能够使用socket.io ,所以我没有任何好的生产示例(并且因为我即将离开当之无愧的假期我没有时间把它们放在一起)。 There are some good examples though: 虽然有一些很好的例子:

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/ dir)。

Good luck! 祝好运! (And thanks to Josh3736 for rectifying my errors.) (感谢Josh3736纠正我的错误。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM