Latest Node.js Recipes

Express js error handling

I'm trying to get error handling running with express but instead of seeing a response of "error!!!" like I expect I see "some exception" on the console and then the process is killed. Is this how error handing is supposed to be setup and if so is...

Read the entire recipe Recipe added 32 minutes ago

Create Document with unique property in Mongoose

I'm trying to save a document with a unique property across the collection.

So if someone tries to save a kitten with {name: 'kitty'} and that name already exisits it fails and retries with {name: 'kitty1'} (then 'kitty2' etc).

I really can't figure...

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

setTimeout in Node.js loop

I'm a bit confused as to how setTimeout works. I'm trying to have a setTimeout in a loop, so that the loop iterations are, say, 1s apart. Each loop iteration makes an HTTP request and it seems like the server on the other end can't handle that many...

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

What is the right way to deal with Mongodb connections?

I try node.js with mongodb (2.2.2) together using the native node.js drive by 10gen.

At first everything went well. But when coming to the concurrency benchmarking part, a lot of errors occured. Frequent connect/close with 1000 concurrencies may...

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

Entering data with MongoDb and Node.js

So I'm trying to enter data into a mongodb collection with node. As far as I can tell I have access to the collection.

var collection = db.collection("whatsGoingOnEvents");
console.log("hitting stream");
var stream =...

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

Socket.IO transmitting a array per emit(...)

I am trying to build a array in my socket io server and sending it to the client.

 var roomList = io.sockets.manager.rooms;

    // new Array to save the clients per room
    var clientsPerRoom = new Array();

    //for (var i = 0; i <...

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

NodeJS/Express: implicitly push data from custom middleware into render call

I'm using NodeJS/Express3.

I have some custom middleware which retrieves some data from a database, based on cookie and URL.

I want to have access to this data in the base template (used for all pages).

I can do this explicitly by attaching the...

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

How to Push data from C# to ZeroMQ and Pull from Node.JS or vice-versa?


I am trying to send a data (say String type) from C# —Āonsole application to Node.JS server through ZeroMQ.


Using clrzmq for c# and ZeroMQ libs for C# and Node.JS respectively

I am able to perform push-pull from Node.JS,...

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

How to configure user environment to use the latest node runtime

We initialize node.js using nvm use 0.10.0 How to configure user environment to use the latest node runtime, so that the bash shell gets initialized when I launch a new terminal in Ubuntu

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

Creating an html document which can be manipulated with the jquery node.js module

I have a string of html from a $.ajax() request (using the jquery node.js package) that I want to scrape data from. From the documentation in the readme it would seem that var $ = require('jquery'); creates a empty html document. Is it possible to...

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

AJAX Helper for NodeJS, instead of jQuery?

I just want to do something like $.get/post on a server side script. Is there a better way instead of including the whole jQuery? I prefer not to use the messy get xml http requests stuff manually either.

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

If crypto.randomBytes is cryptographically strong, is it safe to use as an RNG (and how?)

So in the docs, crypto.randomBytes is described as "Generat[ing] cryptographically strong pseudo-random data". I assume it read off dev/random via openssl.

Now, does that mean it's safe to scale the random byte range to an integer range, as described...

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

Is Mongoose query result read-only?

How can I modify an object returned by a Mongoose query?

Assume we have the following schema:

var S = new mongoose.Schema( { 'name': String, 'field': String } );

I do the following query and modification to the result:

var retrieve =...

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

Mongoose not removing objects from collections

Mongoose shows a strange behavior. The following lines of code first delete a collection entirely, then create a new object, and finally delete the collection again.

var obj = new Model({ name: 'my object'...

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

How do I, in nodejs, set up a Mongo viewer (like Mongo-Express) on the same port as my main app?

I'm new to just about everything with regards to nodejs and Mongodb but I managed to get a simple app up and running and now I want a database viewer. I found Mongo-Express and installed it on my localhost where it works fine. The viewer is configured...

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

How come Q deferreds are so slow on Node.js?

So I created this simple test server in Node.js
Whenever I do a direct response, I get 2200 requests/second (fast!). When I only wrap a simple Q deferred around it, it drops to 580 requests/second (4 times slower!). Can anybody explain that huge...

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

Update multiple docs with different values

Don't think this is at all possible but wanted to check.

If I have the following documents:

doc1 : {
  _id: 1,
  name: 'John Smith',
  age: 20

doc2 : {
  _id: 2,
  name: 'Jane Smith',
  age: 22

I want the client to be able to pass me both...

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

Trying to turn JSONP data from AJAX response into HTML

The response is coming through to the browser in the developer tools, but I'm having trouble getting the data from the response that I want into workable form.


                pollName: loadedPoll.pollName,

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

How to remove module after &quot;require&quot; in node.js?

Let say, after I require a module and do something as below:

var b = require('./b.js');
--- do something with b ---

Then I want to take away module b (i.e. clean up the cache). how I can do it?

The reason is that I want to dynamically load/...

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

Writing a simple automation test in node.js but having trouble with sync calls

I am trying to write an automation script to test some of my api calls here is a simple example:


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

How do I use external routes.js so I don't have to define my routes in app.js?

I'd like to do a simple link in express. I put into the index.jade

a(href='/test', title='View test page') Test

created in /routes a test.js

* GET test page.

exports.test = function(req, res){
res.render('test', { title: 'Genious test'...

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

Command line output when running application as Windows service

In a Windows 2008 R2 environment, I have a chat application that I'm currently starting from command line:

node app.js

This outputs different information about the app's state on the screen, which I can of course see in the command line...

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

Connection-route of node.js taking wrong parameters informations

I've a problem with my node.js application. I'm using the packages connection, connection-route, and ejs.

My application provides informations to the html page (connected via, these informations are managed by an ejs...

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

Create string key to a complicated json path

I need to access and conduct a series of operations on a big json object. Some things I need to read and alter are very deep into the tree with paths such...

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

Run node.js with different file extension

Is is possible to run node.js with a different extension than .js like: node server.type instead of node server.js?

I have tried require.extensions

require.extensions['.type'] = require.extensions['.js'];

but I get this error:


Read the entire recipe Recipe added 1 day ago