Latest Node.js Recipes

node.js runs concurrently, or does it?

Boys and girls,

i've been messing around with node.js today and I can't seem to reproduce this concurrent magic.

i wrote this rather small server:

var http = require("http");

var server = http.createServer(function(req, res) {


Read the entire recipe Recipe added 12 minutes ago

Return value from a mongodb call from nodejs

How do I return an array from a mongojs query. I have the below query executed with nodejs, into a locally running mongodb database. Mongojs is handling the transaction. My question is; how can I return an array from the below function call.


Read the entire recipe Recipe added 1 hour, 17 minutes ago

How to compare two fileStat.mtime in nodejs?

See the code:

var fs = require('fs');

var file = "e:/myfile.txt";

fs.stat(file, function(err, stat1) {
  fs.stat(file, function(err, stat2) {
    console.log(stat1.mtime == stat2.mtime);

Read the entire recipe Recipe added 2 hours, 17 minutes ago

Socket.IO on subdomains with Express.js vhost

I have two Express.js apps running on my server.

A plain vanilla app called "main-app" and another that uses Socket.IO called "socket-app".

I have "main-app" running at "" & "socket-app" running on a subdomain at ""


Read the entire recipe Recipe added 3 hours, 12 minutes ago

Package manager NPM and node.js Integrity

I have just created a new package on NPM (which was surprisingly easy) and I am interested in how the integrity of packages is maintained. Can anybody publish a new version of a package, or is this restricted to just my user account?

If anyone can...

Read the entire recipe Recipe added 4 hours, 12 minutes ago Identify User for Socket

I write some code example that identifi connected users via So now I must write a code on index page to comunicate with users.

The code is below and HOW to send a message to user[1] "Welcome" and for user[2] "HI men" and also limit...

Read the entire recipe Recipe added 5 hours, 17 minutes ago

Can I use node.js as a REPL shell?

This is a noob question.

I am trying to use node.js as a JavaScript REPL (read-evaluate-print loop) shell to work with JavaScript interactively. Unfortunately I can define neither variables nor functions.

 var x = 'abc'
 function f()...

Read the entire recipe Recipe added 6 hours, 17 minutes ago

Node.js shrinkwrapped package.json causes npm install to update new versions regardless

I've got a npm-shrinkwrap.json and a package.json in a git branch called "deployment".

On my servers, I fetch and merge this deployment branch from github. This ensures that my servers have the latest deployment version.

Because the node_modules...

Read the entire recipe Recipe added 7 hours, 16 minutes ago

What's the commonly accepted pattern for representing state in Node.js

For example in classic object oriented programming I might have a class School which has an array of String representing students (not the ideal data structure but for illustration purposes only). It might look something like this

class School {

Read the entire recipe Recipe added 8 hours, 18 minutes ago

Sending an array from websocket to clients

I'm having an issue sending an array from the backend js to the client side.

I've tried the following on the server side:

for (var i=0; i < clients.length; i++) {

for (var i=0; i < clients.length; i++) {

Read the entire recipe Recipe added 9 hours, 11 minutes ago

Why using $self in EventEmitter.prototype.once in node...?

I am looking at the implementation of once() in event.js:

EventEmitter.prototype.once = function(type, listener) {
  if ('function' !== typeof listener) {
    throw new Error('.once only takes instances of Function');

  var self = this;

Read the entire recipe Recipe added 10 hours, 18 minutes ago

Uploading a file programmatically, from node.js, to another web server

I need to push a file from a node.js app, to a web server running elsewhere, which accepts files via the typical upload mechanism. For instance, say the receiving server has a page with a form like this:

<form enctype="multipart/form-data"...

Read the entire recipe Recipe added 11 hours, 18 minutes ago

Node.js, restify and proper routing

I'm still wrapping my head around Node, but I have a very simple question. I see a lot of node examples where people are declaring their routes and all their logic in a single app.js file (or sometimes splitting them off into subfiles).

My question...

Read the entire recipe Recipe added 12 hours, 13 minutes ago

NodeJS everyauth get access token

I am trying to get the access_token for a user using the FB API + everyauth module for NodeJS, and then I will make serverside graph API requests using the Facebook_Graph_API module to actually make the requests (which requires an access...

Read the entire recipe Recipe added 13 hours, 16 minutes ago

How does Google Analytics avoid Spoofing?

So I'm stuck trying to figure out how Google Analytics avoids spoofing. Sure, when you sign up for an account, they make you verify that you own the domain by uploading a file. But you are also given some script tags with a unique public code...

Read the entire recipe Recipe added 14 hours, 16 minutes ago

Saving Images to S3 from External URL with Node.js and MongoDB

I'm trying to save the images from a third-party API to my own S3 bucket using Node.js and MongoDB. The API provides a URL to the image on the third-party servers. I've never done this before but I'm assuming I have to download the image to my own...

Read the entire recipe Recipe added 15 hours, 11 minutes ago

Handling Node.js Async Returns with &quot;require&quot; (Node ORM)

I'm using the Node.js ORM module :

I'm able to create a model by doing this:

    var orm = require("orm");
    var db = orm.connect("creds", function (success, db) {
        if (!success) {

Read the entire recipe Recipe added 16 hours, 16 minutes ago

Nodejs grunt obfuscate

I'm using Nodejs grunt module. I know grunt min option minifies a files. But now I need to obfuscate files like google closure compiler. Does grunt have that feature?

Read the entire recipe Recipe added 17 hours, 12 minutes ago

What is a good way to store binary files on Heroku?

I am using Node.js on Heroku, and use MongoHQ for data.

My app needs to be able to store image files, that are uploaded from the user or pulled down from other sites. I'm not looking for add-on to do image processing or the like, I just need to be...

Read the entire recipe Recipe added 18 hours, 16 minutes ago

How can node.js sockets be linked to a page so text typed when connected to sockets' port appears on page?

I'm familiarizing myself with node.js and after seeing Ryan Dahl's example where he builds a super-simple chat server, i decided to try to create a setup where one port listens for inputted text and sends it to an webpage that's listening on another...

Read the entire recipe Recipe added 19 hours, 11 minutes ago

Android SocketIOClient error: &quot;Scheme 'ws' not registered

I have a node.js/ server and I wanted to create an Android app to comunicate via websockets. I found a library: But when I try to use the sample code (Socket.IO Usage):

SocketIOClient client = new...

Read the entire recipe Recipe added 20 hours, 14 minutes ago

Validating template variables in JavaScript

Is there a best case practice for validating when using a templating engine?

I'm currently building a site with node.js and couchdb, and I'm using express with ejs as a framework and templating engine respectively. It's possible that I end up with...

Read the entire recipe Recipe added 21 hours, 16 minutes ago

Node package ( Grunt ) installed but not available

I'm trying to build a github jquery-ui library using grunt, but after running npm install I still can't run the command according to the readme file. It just gives No command 'grunt' found:

james@ubuntu:~/Documents/projects/ad2/lib/jquery-ui$ grunt...

Read the entire recipe Recipe added 22 hours, 18 minutes ago

Node.js For-Loop Inner Asynchronous Callback

I have to following function that assesses if a password is in the user's password history. Here is the code:

isPasswordInPwdHistory : function(redisClient, userId, encPassword, cb) {

        var key = 'user:' + userId + ':pwdhistory';

Read the entire recipe Recipe added 23 hours, 16 minutes ago

Testing javascript with Mocha - how can I use console.log to debug a test?

I am using the javascript test-runner "Mocha".

I have a test that is failing, so I would to debug it using console.log.

But when the tests are run, there is no output (only the test results from Mocha). It seems like Mocha has captured and...

Read the entire recipe Recipe added 1 day ago