使用 Node.js 构建网站的最佳实践 [英] Best practices of building a website using Node.js

查看:19
本文介绍了使用 Node.js 构建网站的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was wondering how one would go about developing a website from scratch with 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. 做了很多 AJAX
  2. 直截了当
  3. 相对较小
  4. 连接到...假设一个 MySQL 服务器

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

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:

  • includes/db/ 里面有connect.php用来连接db,一个有常用db相关函数的文件等等
  • includes/layout/ 里面有 footer.php、header.php 和其他布局相关的东西
  • includes/users/ 处理与用户相关的操作
  • 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

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

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

<?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');
?>

我想知道 Node.js 中可能有哪些相似之处 - 我正在寻找一种方法来实现这一点,即简单快速直截了当 尽可能.

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.

如果答案不简单,我很想推荐一本书,我不介意阅读.

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

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

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.

推荐答案

从坏消息开始:As Node.js 是一项非常年轻的技术,我认为您会发现创建一个成熟的网站和维护/运营它的过程将与您目前所习惯的非常不同.

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.

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

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

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 Package Manager, NPM 有很多很好的工具和框架来扩展 Node.js 的基本功能,使其适合创建网络服务器.

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.

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

Most notably would be the Express Framework which 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 建立在 Sencha's Connect 之上.Cookie 和会话实际上由 Connect 提供支持.Express 可以简化您的路由并处理视图/部分.因此,如果您不需要 Express 附带的所有功能,您可以选择 Connect.

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.

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

If you like to use templates for these partials, the Jade Template Engine can speed things up for you. Though Josh3736 points 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.io based applications only, so he's a better source than me when it comes to templating).

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

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. MongoDB via Mongoose is the popular way to go.

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

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

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

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. See this SO question of mine for more information.

结论:

我想知道的是:

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

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.

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

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.

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

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 的主要生产用途是能够使用 socket.io,所以我没有任何好的生产示例(因为我即将离开一个当之无愧的假期,我也没有时间把它们放在一起).不过也有一些很好的例子:

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:

  • Setup and deployment using Express and Jade
  • A very complete blog example using Express, Jade and MongoDB
  • Combining Restify (an extension of Express), Backbone.js and Mongoose

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

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

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

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

这篇关于使用 Node.js 构建网站的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆