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

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

问题描述

我想知道如何用Node.js从头开始开发一个网站。
我理解我可以可能这样做,但我对最佳设计实践感兴趣。



<我需要这个理论网站:


  1. 做了很多AJAX

  2. 非常简单

  3. 相对较小

  4. 连接到...让我们说一个MySQL服务器

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




  • 包含/ db / ,其中包含用于连接数据库的connect.php,具有常见数据库相关功能的文件等等。

  • 包含/ layout / 其中包含footer.php,header.php和其他布局相关内容

  • 包括/ users / 来处理用户相关的操作



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

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

//与页面相关的其他php或html或相关内容

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

我想知道Node.js中可能有什么相似之处 - 我正在寻找一种方法来实现这一目标这是简单快速直截了当



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



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

解决方案

从糟糕开始新闻:由于 Node.js 是一种非常年轻的技术,我想你会发现创建一个完整的网站并维护/操作它的过程将与您目前习惯的非常不同。



Josh3736 补充说:一旦弄清楚如何Node.js及其各种软件包(Connect,Express)工作,我发现你可以很快开发新网站。



目前存在的粗糙边缘在Node.js中,结合其开发的快节奏,所涉及的所有模块都会使事情复杂化,并使事情变得不那么简单,快速和直接。



解决了这个问题,这是个好消息:



节点包管理器,NPM 有很多很好的工具和框架来扩展Node.js的裸机功能,使其适合创建一个网络服务器。



大多数值得注意的是 Express Framework ,其中包含运行网络服务器所需的几乎所有内容(包括cooki es,sessions和path routing)。另外Express支持部分,它负责你的页眉和页脚包含。



Express建立在 Sencha的连接。 Cookie和会话实际上由Connect提供支持。 Express可以简化您的路由并处理视图/部分。因此,如果您不需要Express附带的所有铃声和口哨,您可以转而使用Connect。



如果您想使用这些部分的模板,< a href =http://jade-lang.com/ =noreferrer>翡翠模板引擎可以为您加速。虽然 Josh3736 指出Jade速度慢且空白重要。可以在这里找到更完整的概述,其中包括他的最爱,< a href =https://github.com/olado/doT =noreferrer> doT 。 (我个人使用Node.js来处理基于 socket.io 的应用程序只有,所以当涉及模板时,他是比我更好的来源。)



可以使用<一个href =http://nodejsdb.org/db-mysql/ =noreferrer> db-mysql 模块,但是如果你不需要那个,因为你正在访问已经连接的数据现在的系统,我建议使用更多......'现代'的方法,即使用 NoSQL 数据库与大多数Node.js项目似乎一样。通过 Mongoose < MongoDB / a>是最受欢迎的方式。



或者如果它只是存储你感兴趣的对象,那就去 Redis (无论如何你可能会需要它)。



一旦你的网站完成,你将不得不部署它并确保它继续运行。有很多方法可以实现,例如使用内置群集支持或使用功能更友好的 forever npm模块。 请参阅我的SO问题以获取更多信息。



结论



我想要的是:



要求在Node.js中构建网站的最佳实践是,与询问在PHP中构建网站的最佳方式是一样的:100位开发人员会给出100个不同的答案。



NPM拥有各种优秀的框架,可以大大简化所涉及的许多任务,但这一切都基于偏好,哪一种是真正的方法。



正如我所说,Node.js仍然是一种非常年轻的技术,因此没有任何框架或其他工具已经成为事实上的标准;对于你想要做的大多数事情,可能有各种各样的选择,并期望你的代码在更新过程中使用大部分代码时会中断,因为Node.js本身和大多数模块的开发都是快节奏的。你必须跟上。



全部放在一起



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





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



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


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. Do a lot of AJAX
  2. Be very straightforward
  3. Be relatively small
  4. Connect to... let's say a MySQL server

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/ 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

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

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.

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.

解决方案

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

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.

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.

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

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

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.

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

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.

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.

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.

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.

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:

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

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

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

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