Latest Node.js Recipes

Simple Node.js example not working in Windows 7

I am seeing some demos of node js

setInterval(function(){
    console.log('world');
}, 2000);

console.log('hello');

I have installed node js for Windows, this prints hello and then world when I run it from command prompt as per instruction at a...

Read the entire recipe Recipe added 2 minutes ago

PSQL: Type coercion for large numerics

column_with_very_big_numerics is numeric(30,0)

SELECT
    column_with_very_big_numerics
FROM some_table

I am using the pg node.js client. I would the result to come back as a numeric. Because this number is so large that I can't use int...

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

Denying access to partial views with node.js/ express?

How would I go about denying access to a partial views with node.js/express? I have various different views in my single page web app and would like some pages to not be shown unless a user is logged in. How would I go about doing so? I know this...

Read the entire recipe Recipe added 2 hours ago

Behavior of require in node.js

I currently have a database connection module containing the following:

var mongodb = require("mongodb");
var client = mongodb.MongoClient;
client.connect('mongodb://host:port/dbname', { auto_reconnect: true },
      function(err, db) {
         if...

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

A "clean" way to use a different ORM (such as node-orm2) in Sails.js?

I'm reviewing various frameworks for node.js and I'm just now testing Sails.js by writing a small CRUD app. I would like to know if there is an elegant way to use a different ORM other than the one sails includes.

I haven't seen anything in the...

Read the entire recipe Recipe added 4 hours, 1 minute ago

How to convert a string to ObjectId in nodejs mongodb native driver?

I'm using mongodb native driver in a nodejs environment and I need to convert an id string to ObjectId to use it in my update query, how can I do this?

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

Stubbing a class method with Sinon.js

I am trying to stub a method using sinon.js but I get the following error:

Uncaught TypeError: Attempted to wrap undefined property sample_pressure as function

I also went to this question (Stubbing and/or mocking a class in sinon.js?) and copied...

Read the entire recipe Recipe added 6 hours, 1 minute ago

"grunt clock in" instead of "grunt clock --in" <-- Possible?

I'm looking around in the docs and I don't see this as being possible, but I would really like the api to my grunt plugin to be that much easier...

Read the entire recipe Recipe added 7 hours, 1 minute ago

Get computer user's name

I'm writing a plugin for grunt and I need to be able to get the currently logged in user's username whether it be on a *nix or Windows operating system. I've looked around, but haven't found any good way to do this. Is this possible? I saw that it may...

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

How can I handle C like struct union type on Nodejs Buffer?

I'm trying to parse a buffer on Nodejs that makes use of a struct union type, how can I handle this natively on Nodejs? I'm completely lost.

typedef union
{
   unsigned int value;
   struct
   {
      unsigned int seconds :6;
      unsigned int...

Read the entire recipe Recipe added 9 hours, 1 minute ago

Can I $addToSet and upsert an array?

I'm using Mongoose and my Schema looks like:

var WalletSchema = new Schema({
  accounts: [String]
});

My query (Node.js) looks like:

Wallet.update {accounts: account}, {$addToSet: {accounts: account}}, {upsert: true}, (err, updWallet) ->
  asyncCb...

Read the entire recipe Recipe added 10 hours, 1 minute ago

How can I clone a window server-side using jQuery?

I am currently running AngularJS server-side using Node.js and jsdom. It's working great, but I'm trying to optimize performance by caching the DOM generated by jsdom before the AngularJS scope is applied to the page. Trouble comes when I try to clone...

Read the entire recipe Recipe added 11 hours ago

Zombiejs - fetching contents of links synchronouly

I have been playing with nodejs and zombiejs to fetch some personal data from a site. Unfortunately I am stuck at a point where zombiejs only gets me the data from first link and then hangsup.

The steps I follow are-

  1. Go to to the base url
  2. Get the...

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

My node.js program isn't working the way I expect javascript to

I've been using javascript for years, and gotten pretty good at it since I started a web development job, but working with node on a personal project I am having issues.

I create an object

    ;( function( undefined ) {
    'use strict';

    var...

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

Call npm.info(modulename) without printing result to stdout

With a local installation of npm in ./node_modules/npm and inside a default node shell,

var npm = require('npm');
npm.load({}, function (err, res) {
    npm.info('tennu');
}

the result of npm.info() will echo to stdout the same stuff that it would...

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

Node packages not building on Windows 8.1 - Missing Microsoft.Cpp.Default.props

NPM packages are not building on Windows 8.1 - failing with following error,

error MSB4019: The imported project "C:\Microsoft.Cpp.Default.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on...

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

Node.js and sqlite3 npm module: obtain all rows in database#each

I am using node.js version 0.10.24 and the npm module named sqlite3 version 2.1.19. I test my scripts on an OS X 10.9.1 box, using a terminal window. The following script:

var sqlite3 = require('sqlite3').verbose()
var async = require('async')
var...

Read the entire recipe Recipe added 16 hours, 1 minute ago

Populating a dropdown list in form with another models data in nodejs

I am trying to populate a dropdown list for a form for one of my models (cars), with data from another model (colours), but cannot seem to figure out how to do this. I need to somehow call the list of colours into the dropdown in the cars.jade file,...

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

Cannot install node-xmpp or node-expat due to node-gyp rebuild error

I am just starting out with node.js and have hit a wall trying to install node-xmpp and node-expat. It seems node-gyp rebuild is the common cause.

I am on Ubuntu 10.04. After running npm install node-expat, I get the following output:

npm http GET...

Read the entire recipe Recipe added 18 hours ago

Mongo $in query works in mongo but not in sails

Basically the query works in mongo but not in sails controller:

    db.membermodel.find({identifier:{$in:["2","3","4"]}); // works

    MemberModel.find({
        identifier:{$in:["2","3","4"]},
    }).then(function(members){
        // doesn't work
...

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

Unit testing node.js model code accessing mongodb, without actually accessing the database

For example, I have the code below:

var db = require('./_mongo.js');
module.exports = {
    check: function (cb) {
        var content = {};
        content.collection = 'counters';
        content.query = {_id: 'ping'};
        content.columns =...

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

NodeJS - Passport password appears as plain text in DB

Probably I am missing something here.

Got an Express server with MongoDB and i'm using passport to authenticate. I'm using one of the standard code example to signup and it seems ok, but I can see the password I type in the password field (plain...

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

Get nodejs function definitions

I want to gather the source code of defined functions from a js file. The functions can be complex and have many opening and closing brackets so using regex will be hard. I only need functions that are callable later on in the code, like the...

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

Node.js mongoose module failing on Heroku

Starting a couple days ago, our site http://emaildelivery.herokuapp.com displays an Application Error:

"An error occurred in the application and your page could not be served. Please try again in a few moments.

If you are the application...

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

allong.es variadic function not found

I get the following error:

node.js:201
    throw e; // process.nextTick error, or 'error' event on first tick
          ^
TypeError: undefined is not a function
    at /home/ubuntu/nodejs/test.js:4:10
    at factorial...

Read the entire recipe Recipe added 1 day ago