background preloader

Node.js

Facebook Twitter

Getting Started With Node.js, Express, MongoDB. Introduction to Express. Asynchronous Iteration Patterns In Node.js. Some patterns are hard to grasp, specially when programming asynchronously like you have to when you’re doing IO on node.js.

Asynchronous Iteration Patterns In Node.js

For example, let’s suppose you had to program the following routine: Insert a collection of objects on the database and then, when finished, call a callback. So, if you had to write this in a synchronous fashion you would do something like this: function insertCollection(collection) { for(var i = 0; i < collection.length; i++) { db.insert(collection[i]); } } So, since we are using node.js, db.insert is most probably asynchronous.

I have seen some obviously wrong implentations like this one: function insertCollection(collection, callback) { for(var i = 0; i < collection.length; i++) { db.insert(collection[i], function(err) { if (err) { throw err; } }); } callback(); } The problem with this one is obvious: callback is called right after launching all the db.inserts on the background, not leaving them a chance to finish. Another approach would be this one: Serialization. Node.js async in practice: When to use what? When I started out using node.js and async I didn't find any good/thorough resources on how to really use the async module.

Node.js async in practice: When to use what?

That's why I decided to make a little cookbook about it. What is async solving? An antipattern Async and other similar Node.js control flow modules seek to simplify code such as this: Since the two db calls are asynchronous we don't know which one of them is going to finish first. And what happens when we need to add another asynchronous task? This is where async comes to our aid and makes the code sane to look at and easy to maintain. Important note about callbacks and errors One thing that wasn't obvious to me when I first looked at async, was the way callbacks are used.

Generally all the async functions take a set of tasks to perform as argument. Besides the set of tasks the async functions also take a callback function as argument, let's call this the final callback. Example: The 4 lines of error handling gets pretty tedious. If (err) return callback(err); How To Node - NodeJS.

Backbone.js

Jade - Template Engine. Руководства по Node.js. 40+ Resources for the Node.js Developer. The Node Beginner Book » A comprehensive Node.js tutorial. Creating a REST API using Node.js, Express, and MongoDB. Своё приложение на Node.js с хранением в Dropbox – это просто. Несмотря на то, что главным моим хобби так и остаются роботы, я трачу немало усилий, чтобы оставаться в трендах своей основной стези – программирования.

Своё приложение на Node.js с хранением в Dropbox – это просто

Волей судьбы недавно удалось познакомиться с Node.js, я узнал о его web фреймворке express, подружился с новым для себя template engine Jade и в довершение ко всему связал все это с папкой в Dropbox. В этом посте я постараюсь коротко рассказать, как можно организовать web-сервис для хранения файлов, используя лишь бесплатные решения. Всех заинтересованных – прошу под кат. Подготовим плацдарм Итак, нам понадобится:Node.js установленный на локальной машинеАккаунт в DropboxСервер Node.js приложений (если захочется запустить сервис не только локально) Если с первыми двумя пунктами все должно быть понятно, то на третьем мне бы хотелось остановиться чуть подробнее. Создадим проект Локально С чего-то ведь надо начинать. Npm install Тем временем в Dropbox Для того, чтобы запустить скрипт просто набираем в командной строке: node dbox-init !!! Запуск. Node.js v0.8.17 Manual.

Node.Js And Stuff. Download demo app Contents Introduction It has been a while since I wrote an article here at CodeProject, so I apologize for that.

Node.Js And Stuff

I have been pretty busy doing, um nothing I guess. Anyway late last year I found a bit of spare time, so I looked at my to-do / must learn list, and sitting there glaring at me was "Node.Js" in 60 ft letters. So this article is me playing around with Node.js a bit, and coming up with a small demo project.