Latest Node.js Recipes

Node.js: Extracting the image urls of a webpage

I'm trying to get the images of a given webpage. I've tried using the modules request and jsdom as follows:

var request = require('request'),
    jsdom = require('jsdom');

request({ uri:'http://www.google.com' }, function (error, response, body) {
 ...

Read the entire recipe Recipe added 20 minutes ago

Mocking Postgres for unit tests with Sinon.js in Node.js

I am having trouble getting my head round how i can use sinon to mock a call to postgres which is required by the module i am testing, or if it is even possible.

I am not trying to test the postgres module itself, just my object to ensure it is...

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

Using everyauth with restify

I'm trying to use everyauth to handle authentication for a rest api created with restify. But can't find a starting point. I'd expect to be doing something like:

var restify = require('restify');  
var everyauth = require('everyauth');
var server =...

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

How do I post a multipart request with request.js

The title simply says it. I can not figure out how to send a a couple of fields vial multipart.

I understand that there is an implementation with https://github.com/felixge/node-form-data

I have all my fields available and would just to post it as...

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

Extract .zip and then store contents in S3 with Node.js

Most questions regarding NodeJS, S3 and Zips are the opposite of what I would like to do. Upload a zip, and extract the files onto S3.

I have a quick app running on an Amazon EC2 instance, with Node.JS that accepts a file upload via a POST request....

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

Document Polymorphism in a MongoDb collection using Node.js / MongooseJs -

I need to maintain a collection 'users' of [User] documents for implementing an authentication mechanism for my site.

[User] ~ {color: string, username:string, password-hash:string, password-salt: string}

Additionally, each [User] can be one of 3...

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

Recursively create directories from a multi-level JSON (with async.js)

DEFINITION

File#createDirectoriesFromJSON (json, cb);

json: JSON object.

cb: Function. Parameters: error (Error), created (Boolean, true if at least one directory has been created).

Supose that the File class contains a property named _path that...

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

Passing messages from Node.js to external systems?

I'm completely new to Node.JS, if I want to pass messages to external systems using a message/event/servicebus.. what alternatives are there that integrates well with Node?

The messages are just JSON strings, and external systems can be whatever,...

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

How to do pass-thru uploads to s3 on Heroku with node.js?

Here's my current code. I'm using express and knox, and I don't think I'm doing anything unusual, but s3.putFile is responding 400 status code but null error, and the file does not get uploaded.

var express = require('express');
var knox =...

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

Querying nested documents using Mongoose (MongoDB)

I am starting out with mongodb and having hard time trying to query nested documents. I have two schemas:

var LinkSchema = new mongoose.Schema({
    url: String,
    name: String
});

var UserSchema = new mongoose.Schema({
    name: String,
   ...

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

passing parameters to Node.js async waterfall

I need to pass some parameters to the initial function of async waterfall(). The proposed method https://github.com/caolan/async/issues/14 does not work for me as I need to pass it the response from an ExpressJS function

exports.categories = (req,...

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

Conditionally call async function in Node

I have the following example code - the first part can result in an async call or not - either way it should continue. I cannot put the rest of the code within the async callback since it needs to run when condition is false. So how to do...

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

Nested NSDictionary & NSArray with AFNetworking not working properly

NSDictionary *customerDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"blah@blah.com", @"email", @"1", @"facebook", nil];
NSArray *customerArray = [NSArray arrayWithObjects:customerDictionary, nil];
NSDictionary *parameters = [NSDictionary...

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

What would be the best possible way to generate API keys and hash passwords in Nodejs?

I am using my own authentication and sort of worried about the present security of my API. What would you suggest would be a good way to generate API keys? And what would be a better way to hash the password?

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

Asynchronous wait for an condition to be met

In my code I have three variables whose value depend on an external function call, they can be set to true in any moment and in any order, so, they are more like flags.

I need a function to be only once these three variables are set to true.

How can...

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

Node.js Heap Snapshots and Google Chrome Snapshot Viewer

Is there any way to take a heap snapshot from a running Node.js process and then load it into the Google Chrome profiles viewer? It would be very awesome to be able to use the really useful Chrome profiles tab to view Node.js snapshots.

If it is...

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

Getting the page title from a scraped webpage

var http = require('http');
var urlOpts = {host: 'www.nodejs.org', path: '/', port: '80'};
http.get(urlOpts, function (response) {
response.on('data', function (chunk) {
var str=chunk.toString();
var re = new...

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

What is app.configure('production')

I am a newbie in NodeJS, what to do with the below setting which is given in the Express documentation?

app.configure('production', function(){
  app.set('db uri', 'n.n.n.n/prod');
});

Should I just copy and paste or should I need to change the...

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

Error while using Node.js.

This is my model.

UserApiSchema.statics.createApi = function(user,fn){
  var instance = new UserApi();
  instance.user = user;
  instance.apiKey = "asdasdacasdasasd";
  console.log("username is " + user.username);
  instance.save(function(err){
   ...

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

Sort in MongooseJS 3.3.1 in ascending and descending order

I have REST based API in NodeJS and MongooseJS ODM. To GET the list of departments URL is http://localhost:25718/Department?SortBy=Name&SortType=asc(desc-for descending). And sorting code is

 var Query = Department.find();
 if(req.query.SortBy) {
   ...

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

NodeJS: process.nextTick vs Instant Callbacking

I write lots of modules which look like this:

function get(index, callback) {
    if (cache[index] === null) {
        request(index, callback); // Queries database to get data.
    } else {
        callback(cache[index]);
    }
}

Note: it's a bit...

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

insert some data after response on node.js

I wrote a website tracker for internal use, since Google Analytic don't meet the requirement.

I use node.js with Express to develop that tracking system. Everything works well so far. But I want to confirm is, if there is some problem behind if I...

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

Two way communication with server side objects for web-application development

Background

My background is high scale object oriented middleware and Applications development for embedded devices and desktops with C++. Now we need to create a high scale web-app for our startup.


Question

Request-response based and...

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

Set content length of request based on content in node

How to set the content length of the request based on the content?

For example:

POST /Display HTTP/1.0
Content-Type: application/json
Content-Length: 125

{"QueryReq":
  {
    "Tid": "Tid-123456",
    "SessionId" : "1350711351232058820" 
  }
}

On...

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

js Function queue

So essentially what I'm trying to do is have a queue of functions that I can add to. These functions are suppose to be drawing things to a canvas like in an requestAnimationFrame.

What I've tried:

var drawQueue = [];
function animate() {
    while...

Read the entire recipe Recipe added 1 day ago