Latest Node.js Recipes

MongoDB: Update property of subarray just updates the first element

The matching element looks like that:

{
    "_id": {
        "$oid": "519ebd1cef1fce06f90e3157"
    },
    "from": "Tester2",
    "to": "Tester",
    "messages": [
        {
            "username": "Tester2",
            "message": "heeey",
         ...

Read the entire recipe Recipe added 23 minutes ago

How to send an image via Node.js/Express, so it is cached permanently?

What are the required http headers to tell the browser to cache an image forever, when sending it via Node.js?

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

MongoDB Query: Exclude objects from child array?

I'm working on a simple login system for my NodeJS application. For this I have created a structure where one object, a "corporation", holds an array of users. I've done because I plan to use the corporation object to store application session...

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

Grunt watch error - Waiting...Fatal error: watch ENOSPC

Why do I get the Waiting...Fatal error: watch ENOSPC when I run the watch task ? How do I solve this issue?

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

Mocha.js: to run "after" hook even if test suit fails

Is it possible to run "after" hook even if one of tests (suite) fails?

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

Why am I getting a "RangeError: Maximum call stack size exceeded" error?

I have my website hosted by Node.js with html and when I POST from a form below:

 <form class="validateForm" id="registerform" method="POST" action="/signUp"  accept-charset='UTF-8'>
            <fieldset>
                <legend>Register</legend>...

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

Folder structure for both server side and thick client web app

Currently I am using Node.js for the backend and either extjs or backbone for the client and I am now completely confused on the folder structure.

Using express my folder structure is as follow

appname
  |--controllers
  |--models
  | ...

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

Why does this callback with a timeout fails to return?

function one(num, callback) {
  return callback(num);
};

function two(num) {
  // return num + 99; << this works
  setTimeout(function() { return num + 99; }, 1000); // << this doesn't
};

console.log(one(55, two));

The result of return within a...

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

Bus Error on Mongodb MongoClient.connect() for Raspberry Pi ARM

I have tried executing this node script on an x64 computer, and it appears to work fine.

However, the same script reports a bus error when run on the Raspberry Pi. It gives a [6047.715610] Unhandled Fault: alignment exception error, with the error...

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

Why is using try / catch a bad idea in this scenario?

I've been working on hooking up mongoDB to a node.js server. I've got the code all neatly put away, however it takes about 5~ seconds to connect, and if a request for an insert or a query comes before that, the server will crash.

My first instinct...

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

Difference between equal, deep equal, and strict equal

Looking at this page, I was wondering exactly what's the difference between equal, deep equal, and strict equal?

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

NodeJS TCP Socket does not show client hostname information

lets say I've created a simple node.js TCP Socket server as follows.

var server = net.createServer(function (socket) {
  socket.end("goodbye\n");
});
// listen on port 80, like a webserver would, and configured to accept connections to multiple...

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

How to return something async with require?

I'm a little new to this so bear with me...

app.js:

var testVariable = require('./test');
console.log(testVariable);

test.js:

setTimeout(function() {
    exports.something = "HELLO!";
}, 10000);

In this scenario, the output is {}. If...

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

Nock intercepts the HTTP POST requests and hangs during testing Mocha/Node.js module in CoffeeScript

I am wondering if you have experienced the situation where the 'nock' module intercepts the http request but stops without any logging...at least, that appears to me. I may be wrong. Here is the test:

...
suite('Successful SAVE: ', function(){
   ...

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

significance of node.js port choice

I am a bit new to server side scipts. I am fairly capable with javascript so I decided to take a look at node.js as opposed to php or python ect. Correct me if I am wrong but it seems that when I code my webserver I may freely choose the port number I...

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

Create an empty promise

I have a function in a chain of promises that may or may not do something. E.g.

  getYear().then(function(results){
    if(results.is1999) return party();
    else return Q.fcall(function(){/*do nothing here*/});
  }).then(sleep)

Where getYear,...

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

Node.JS Can I sandbox or limit what a Function can access?

I have a Node.JS instance that dynamically inserts Javascript snippets to be executed from a Database where they are stored as strings. My problem is that I need to be able to Sandbox the executed Javascript from getting access to anything that can...

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

nginx as a proxy for NodeJS+socket.io: client not receiving notifications

I have nginx configured to proxy websockets like this:

location /socket.io {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

}

The clients connect fine but...

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

Mongodb, find if a collection is empty, node.js

I am new to node.js, and heroku, I built a small app that uses node.js and retrieves some data from a mongodb instance. I setup the whole thing but my problem is I think a simple syntactical issue with mongodb.

I need to know on starting the app that...

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

Is there a high-level DynamoDB module for Node.js that supports secondary indexes?

I see quite a few DynamoDB modules on npmjs.org. I am guessing that all of them pre-date the introduction of secondary indexes and so don't support them. Does anyone know of a high-level DynamoDB module for Node.js that supports secondary indexes?

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

How to HTTP get request with parameters in node.js?

Im working with NODE.js Im using 'request' module :

It works:

var request = require('request');
request('http://www.site.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
});

When I...

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

Using less-middleware to compile multiple source CSS files into one?

In my Node.js application, I am using less-middleware to compile the LESS files into CSS files. Works great with single files:

app.use(require('less-middleware')({ src: path.join(__dirname, '../', '/public/css') }));

However, I was wondering if it...

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

How browserify makes Node.js modules to work in the browser?

Ii really can't understand some work behind browserify. When using pure JavaScript modules it seems "easy" to convert it to browser code, even if with many dependencies.

But browserify does more:

Many npm modules that don't do IO will just work...

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

Reference Socket.io fron Node server in Offline App with Require.js

I am trying to use Socket.io hosted by a Node server. I am using Require.js to manage dependencies. My webapp is Offline capable.

When the webapp is offline, and cannot contact the node server, require.js throws an error because it cannot find the...

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

Session cookies do not change using ajax and nodejs server

I want to change a session cookie after an asynchronous request but no matter what I tried I keep failing. My request is as follows:

$.ajax({
    type: "POST",
    url: "/setStatus",
    data: { userId : _userId, token: _token, tokenSecret :...

Read the entire recipe Recipe added 1 day ago