Latest Node.js Recipes

Node.js flow control

My question is the following: I want to return the result returned by the callback.

exports.process_logs = function(file, process_func, process_parsedLog) {
var logs = [];
var log = null;
var counter = 0;
fs.readFile(file, 'utf8',...

Read the entire recipe Recipe added 22 minutes ago

How to split and modify a string in NodeJS?

I have a string :

var str = "123, 124, 234,252";

I want to parse each item after split and increment 1. So I will have:

var arr = [124, 125, 235, 253 ];

How can I do that in NodeJS?

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

Node.js: Track new files in a directory

I would like to track new files in a directory. I used the script given in the documentation: http://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener

var fs = require('fs');
fs.watch('mydir/', function (event, filename) {
   ...

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

moment.js - UTC does not work as i expect it

testing in the node console:

var moment = require('moment');

// create a new Date-Object
var now = new Date(2013, 02, 28, 11, 11, 11);

// create the native timestamp
var native = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(),...

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

require() turns constructor's functions to prototypes?

There is just one behavior of javascript or specifically node.js' require() that I like to understand. I already read this:

Adding new properties to constructor function without .prototype

and this:

The difference between putting functions into an...

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

Are nodejs modules cached across multiple HTTP requests?

The nodejs documentation says

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same ...

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

NodeJS - Can't implement asynchronous function

I'm newbie in NodeJs. This is my code for learning asynchronous function.

//--------------------- MAIN ---------------
console.log("Endpoint 1\r\n");

testThread(1000000000,function (result){
   ...

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

how to use domains properly in nodejs

I try to use domains core module instead of process.on('uncaughtException'), to catch error

function setupDomains(req, res, next) {
    var reqd = domain.create();

    reqd.add(req);
    reqd.add(res);
    reqd.on('error', function(err) {
     ...

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

Can't get to my nodejs server through web browser

Alright, so I setup a node.js server quite a while ago on a AWS EC2 micro server. I was completely new to it and followed various tutorials to get it up and running. It used nginx as a reverse proxy (I believe) and the server was listening on port...

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

Using multiple parameters in URL in express

I am using Express with Node and I have a requirement in which the user can request the URL as: http://myhost/fruit/apple/red.

Such a request will return a JSON response.

The JSON data, before the above call looks like:

{
    "fruit": {
     ...

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

.is() doesn't exist in Cheerio?

I need to check if an element is last child like this

Determining if the element is the last child of its parent

But I use cheerio instead of jquery (for lighter weight) on node.js

https://github.com/MatthewMueller/cheerio

It gave me...

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

Running Python on Windows for Node.js dependencies

I am getting into a Node.js codebase which requires that I download a few dependencies via NPM, namely jQuery.

In attempting to run npm install jquery, I keep getting this error:

Your environment has been set up for using Node.js 0.8.21 (x64) and...

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

Trying to dynamically set Pages in Loop with Node.js

I'm trying to do something (which I hope) is simple: receive JSON from the a database and loop through it to create all the necessary page routes in Node.js. As of now, I'm just using stubbed data, but the console.log is always showing the last value...

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

node.js, mongo quick way to hold unique number

I want to let mongo hold an incrementing number for me such that I can call that number and then generate a string from it.

x = 1e10
x.toString(36).substring(2,7)

>>'dqpds'

I have a way to increment the number every time I call it from...

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

Node.js and Sendgrid mailer error on res.end

I'm a bit new to node. I'm using express and the sendgrid api to send an email (collected REST-fully). After sendgrid succeeds or fails, I want to respond with a json object. Here's the sample case:

var SendGrid =...

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

Node spawn child and unref issue

I'm having to call a command in Node and then essentially hand over to that command's stdio, so when spawning a child process and setting the stdio option to inherit I get exactly the behaviour that I'm after.

That said the parent process remains...

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

Mongoose findOneAndUpdate not working

I am trying to use 'findOneAndUpdate' in mongoose and the updated JS object I am sending is not getting saved to mongo. I do not get an error upon saving, but I do get back a null for the updated object. Any ideas what I might be doing wrong? This...

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

how to parse a large, Newline-delimited JSON file by JSONStream module in node.js?

I have a large json file, its is Newline-delimited JSON, where multiple standard JSON objects are delimited by extra newlines, e.g.

{'name':'1','age':5}
{'name':'2','age':3}
{'name':'3','age':6}

I am now using JSONStream in node.js to parse a...

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

How to deliver a node.js + imagemagick tool?

I've developed a node.js tool which uses imagemagick from the command line. That is, exec('my_imagemagick_commands'). What's the best way to deliver that tool to a client using Windows? That is, how can I make a Windows installer that'll install...

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

Can't connect flash to node.js on remote serve EC2r?

I had flash talking to node.js via sockets on localhost, then I migrated it to EC2 instance with linux. Now I'm getting a sandbox security error, I tried using:

Security.loadPolicyFile("xmlsocket://" + host + ":" +  "1337");

But I can't seem to see...

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

How to reverse proxy client POST & PUT requests using node-http-proxy

I'm trying to use the node-http-proxy as a reverse proxy, but I can't seem to get POST and PUT requests to work. The file server1.js is the reverse proxy (at least for requests with the url "/forward-this") and server2.js is the server that receives...

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

Getting a variable out of a nested function in Node.js

I'm using a function to check if a user is already registered on a site. However I can't figure out how to get the value of rows out of the function selectEmail()

I'm trying to store it within temp, but it seems to store it within its own local...

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

Node.js AWS SDK requests not receiving response data.. in one file, not another

I'm certain I'm doing something stupid here, or at least missing something obvious. Just running some very, very basic examples on the official AWS SDK module. I create two empty files, one within the directory of an existing larger Node.js project,...

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

node-gyp and binding,gyp not default_configuration

I am trying to compile a module as release by defaults, I set at buinding.gyp the

  'target_defaults': {
    'default_configuration': 'Release'
   },

But always I got as BuildType="Debug"

gyp info it worked if it ends with ok
gyp info using...

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

Data stream, which way to go?

I am about to start a HTML5 game, with heavy logic in java script, I want to keep some logic at the server side, so that I guarantee that my game will play only at my server.

I decided to chose node.js, as its very fast, I thought about two...

Read the entire recipe Recipe added 1 day ago