background preloader

Essential Node.js patterns and snippets

Essential Node.js patterns and snippets
In this post, I take a look at the different patterns that you need to know when using Node.js. These came from my own coding and from a look at the code behind Tim Caswell’s flow control libraries. I think it is necessary to know how these basic patterns are implemented even if you use a library.. 1.1 Class pattern If the class is long, then instead of doing a single Class.prototype = {…} assignment, it may be split into multiple Class.prototype.method = function () {..} assignments. Reminder: Assign all your properties some value in your constructor. 1.2 Accessing global values from objects // constructorvar Class = function(global, value2) { this.global = global;}// access using this.global in class methods 1.3 Factory pattern // Constructorvar Class = function(value1, value2) { ... }// FactoryClass.factory(value1) { return new Class(value1, "aaa"); }// properties and methodsClass.prototype = { ... }; 1.4 Sharing state between modules 1.5 Singleton class (added Feb 2011) 2.1 Parsing GET 4.

Node.js - Do only what matters Should we use Node for sending static file? Event Database, unit test, how to create external modules that are installable via npm The Node Beginner Book » A comprehensive Node.js tutorial About The aim of this document is to get you started with developing applications with Node.js, teaching you everything you need to know about "advanced" JavaScript along the way. It goes way beyond your typical "Hello World" tutorial. Status You are reading the final version of this book, i.e., updates are only done to correct errors or to reflect changes in new versions of Node.js. The code samples in this book are tested to work with Node.js version 0.10.12. This site allows you to read pages 1-21 of this book for free. Intended audience This document will probably fit best for readers that have a background similar to my own: experienced with at least one object-oriented language like Ruby, Python, PHP or Java, only little experience with JavaScript, and completely new to Node.js. Aiming at developers that already have experience with other programming languages means that this document won't cover really basic stuff like data types, variables, control structures and the likes.

Node.js: Five Things Every PHP Developer Should Know | TechnoSophos Oct 26 2011 I recently started working on a few Node.js applications. Coming most recently from PHP (and Drupal in particular), I found the transition to Node.js to be surprisingly easy. Below I list the five things I think every PHP developer should know about Node.js. 1. Google's browser, Chrome, has a notoriously fast JavaScript engine called V8. This has several positive implications for you, the developer: You don't need to learn a new "dialect" of JavaScript. 2. Unlike PHP, Node.js is not "web centric" (yes, you can run CLI apps in PHP, but that wasn't the original intent). But you can do much more with Node. On the one hand, this is great news. On the other hand, since Node.js isn't HTTP-centric, you may find yourself having to implement code to do things once provided for you by the framework. 3. I love jQuery. But Node's clearly not about browser bling. Having that strong 10-years-in-Java background, I thought that JavaScript's weird prototype system would drive me crazy. 4. 5.

DNode: Make PHP and Node.js talk to each other If you've been following my blog, you might have noticed that lately I've started doing quite a lot of Node.js development alongside PHP. Based on conversations I've had in various conferences, I'm by far not alone in this situation - using Node.js for real-time functionality, and PHP (or Django, or Rails) for the more traditional CRUD stuff. Both environments have their strong points. Node.js is very fast and flexible, but PHP has a lot more mature tools and libraries available. So in a lot of projects it is hard to choose between the two. But now you might not have to. Enter DNode DNode is a remote method invocation protocol originally written for Node.js, as the name probably tells. I started working on the PHP DNode implementation in the Symfony CMF hackday in Cologne a week ago, and got it into a running stage on a train ride from there to Paris. With DNode you can expose Node.js functions to be available on PHP, and PHP class methods to be available on Node. PHP as client Server:

bergie/dnode-php Keep a node.js server up with Forever - — blog.nodejitsu.com One of the great benefits of using node.js is the reduction in dependencies needed to run a production web application. By using the 'http' module we can run a stand-alone web server in node.js without the need for a separate server like Apache or nginx. The caveat of not having to use these servers is that their concerns are now the concerns of the node.js application developer. The concern that we will discuss in this article is that of fault tolerance, or how to automatically restart your server when it crashes or enters an invalid state. Before we dive into how to use Forever, lets setup the 'hello world' web server that we will later run with Forever. The above code starts a web server that will respond to all requests with 'hello, i know nodejitsu'. $ node simple-server.js > hello world running on port 8000 $ nohup node simple-server.js > output.log & [1] 23909 This will start our server process in the background and append all output to 'output.log'. [sudo] npm install forever

Related: