Best practices of building a website using Node.js

Benjamin Gruenbaum picture Benjamin Gruenbaum · Jul 3, 2012 · Viewed 84.4k times · Source

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.

Answer

Rem.co picture Rem.co · Jul 3, 2012

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