Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To start with the bad news: As <a href="http://en.wikipedia.org/wiki/Node.js" rel="noreferrer">Node.js</a> 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. </p> <p><a href="https://stackoverflow.com/users/201952/josh3736">Josh3736</a> adds: <em>Once you figure out how Node.js and its various packages (Connect, Express) work, I found that you can develop new sites very quickly.</em></p> <p>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.</p> <p>Having that out of the way, here's the good news:</p> <p>The <a href="http://npmjs.org/" rel="noreferrer">Node Package Manager, NPM</a> has a lot of good tools and frameworks to expand Node.js's bare bones functionality, making it suitable to create a webserver.</p> <p>Most notably would be the <a href="http://expressjs.com/guide.html#creating-a%20server" rel="noreferrer">Express Framework</a> 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.</p> <p>Express is built on top of <a href="http://www.senchalabs.org/connect/" rel="noreferrer">Sencha's Connect</a>. 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.</p> <p>If you like to use templates for these partials, the <a href="http://jade-lang.com/" rel="noreferrer">Jade Template Engine</a> can speed things up for you. Though <a href="https://stackoverflow.com/users/201952/josh3736">Josh3736</a> points out that Jade is slow and whitespace-significant. A more complete overview can be found <a href="https://github.com/joyent/node/wiki/Modules#wiki-templating" rel="noreferrer">here</a>, which includes his favourite, <a href="https://github.com/olado/doT" rel="noreferrer">doT</a>. (I personally use Node.js for <a href="http://socket.io/" rel="noreferrer"><code>socket.io</code></a> based applications only, so he's a better source than me when it comes to templating).</p> <p>You <em>can</em> connect to MySQL from Node.js using the <a href="http://nodejsdb.org/db-mysql/" rel="noreferrer">db-mysql</a> 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 <a href="http://en.wikipedia.org/wiki/NoSQL" rel="noreferrer">NoSQL</a> database as most Node.js projects seem to do. <a href="http://www.mongodb.org/" rel="noreferrer">MongoDB</a> via <a href="http://mongoosejs.com/" rel="noreferrer">Mongoose</a> is the popular way to go. </p> <p>Or if it's just storing objects you're interested in, just go for <a href="https://github.com/mranney/node_redis" rel="noreferrer">Redis</a> instead (which you're probably going to need at some point anyway).</p> <p>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 <a href="https://github.com/nodejitsu/forever/" rel="noreferrer"><code>forever</code></a> npm module. <a href="https://stackoverflow.com/q/11084279/813718">See this SO question of mine for more information.</a> </p> <p><strong>Conclusion</strong>:</p> <p>What I'm trying to get at is this:</p> <p>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.</p> <p>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. </p> <p>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.</p> <p><strong>Putting it all together</strong>:</p> <p>As I've said, my main production use for Node.js is to be able to use <a href="http://socket.io/" rel="noreferrer"><code>socket.io</code></a>, 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:</p> <ul> <li><a href="http://shapeshed.com/creating-a-basic-site-with-node-and-express/" rel="noreferrer">Setup <em>and</em> deployment using Express and Jade</a></li> <li><a href="http://howtonode.org/express-mongodb" rel="noreferrer">A very complete blog example using Express, Jade and MongoDB</a> </li> <li><a href="http://backbonetutorials.com/nodejs-restify-mongodb-mongoose/" rel="noreferrer">Combining Restify (an extension of Express), Backbone.js and Mongoose</a></li> </ul> <p>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 <code>/examples/</code> dir that seems to be present in most repositories).</p> <p>Good luck! (And thanks to <a href="https://stackoverflow.com/users/201952/josh3736">Josh3736</a> for rectifying my errors.) </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThanks 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
      singulars
    2. COThis 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](https://github.com/joyent/node/wiki/Modules#wiki-templating) -- my favorite is doT, which is [fast](http://jsperf.com/dom-vs-innerhtml-based-templating/53).
      singulars
    3. COFinally, 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.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload