79

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.

2
  • 3
    I would also like to point out that it is very important to test your code using for example mocha -> github.com/visionmedia/mocha. Create small well tested modular code(modules).
    – Alfred
    Commented Jul 4, 2012 at 2:19
  • You may also take a look at Erlang which supports concurrency by default, is faster than node.js and easier than say C or Java.
    – user3644644
    Commented Jun 2, 2014 at 20:10

1 Answer 1

184

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

(thanks to Josh3736 for rectifying my errors.)

11
  • 2
    Thanks a lot, just what I was looking for! Just one more short thing, I'm wondering how some code similar to the code I described in the title might look in node.js ? How would the logical seperation work? Let's say I use express and jade with railyway.js . I'm still struggling to understand how my program would look like. If for (a simple) example I have a index.php file which loads and displays basic data from the database and includes footer/header files, how would that sort of code look like (optimally)? Again, thanks a lot Commented Jul 3, 2012 at 13:56
  • 6
    This is a good answer, but a few points: Express is actually built on top of Connect. Cookies and sessions are actually powered by Connect. Express is what simplifies your routing and handles views/partials. Jade is extremely slow and (on a personal note) I hate whitespace-significant languages. Take a look at some of the other templating engines -- my favorite is doT, which is fast.
    – josh3736
    Commented Jul 3, 2012 at 14:06
  • 4
    Finally, I wouldn't say that using Node is "everything but simple, fast, and straightforward" -- it's just very different than what you're currently used to. Once you figure out how Node and its various packages (Connect, Express) work, I've found that you can develop new sites very quickly. Yes, there are still rough edges, but on the whole you can do things simply and quickly.
    – josh3736
    Commented Jul 3, 2012 at 14:06
  • 2
    Remco, I'd also love some reference to my last comment, if you explain how I would implement the said using node (using good, even if not best, practice) I'll mark this answer. This is the last bit that's bugging me and I'm struggling with this last stage, again, thanks. Commented Jul 3, 2012 at 14:19
  • 3
    I have put together this end-to-end example of how to do a full stack website using the MEAN stack (MongoDb, Express, Angular & Node). You can replace Angular or the client with whatever you are familiar with. I have been told this is the best intro video out there, so perhaps it will help others getting started youtube.com/watch?v=AEE7DY2AYvI Commented Jan 9, 2015 at 22:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.