Latest Node.js Recipes

Why my fs.readFileSync does not work

Why does this code not work? If I comment fs.readFileSync('file.html'); the code works and it creates the file 'file.html' But if I uncomment it the fs.writeFileSync does not work, and the program crashes with the error:

fs.js:427 return...

Read the entire recipe Recipe added 35 minutes ago

Passportjs - How do I request a user's image using the Google strategy?

How can I configure my Google API access to request a user's image while authenticating? Currently 'profile' only contains the following properties after a user has successfull authenticated:

profile.identifier : (string)

profile.displayName :...

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

D3js: how to generate standalone SVG files? (Nodejs)

Given a D3js code, such as:

function () {
    var svg = window.d3.select("body")
        .append("svg")
        .attr("width", 100)
        .attr("height", 100);

    svg.append("rect")
        .attr("x", 10)
        .attr("y", 10)
       ...

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

Mechanics behind Javascript callbacks being asynchronous

I'm having a bit of trouble understanding the mechanics of JS callbacks. I have a fair idea of how callbacks can be used in JS, but I do not understand how a callback is asynchronous.

For e.g., if my understanding is correct, a callback is of the...

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

In App redirect in expressjs using middleware

I am trying to make a middleware for handling url aliases, what I am doing right now is :

// [...]
module.exports = function() {

  return function(req, res, next) {
    // getAlias would get an object {alias:"alias/path",source:"/real/path"} or...

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

asyncjs - eachSeries() doesn't iterate through all items in array

I have the following code which uses async.js

var async = require('async');
var A = [];

for(var i = 1; i < 100; i++)
    A.push(i);

async.eachSeries(A, function(item) {
    console.log(item);
});

I expected this to print numbers from 1 to 100,...

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

Dynamic method call from string with args - node.js

I want to write a little function which receive an array of objects which describe rule(s) to check, using node-validator.

I wrote this:

http://pastebin.com/gWarw98P

The error is at the line 68. I'm using the same example than describe in the...

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

Mongoose or query

I need to find documents based on multiple text fields.

var term = new RegExp(req.query.search, 'i');
.find({ company_name: { $regex: term }});

Using the above works great. However, when I try to add an additional field using

.find(...).or([{...

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

Helper for check all params node.js - sails - express

I want to check my params in my controllers. For the moment, I'm doing like that:

create: function (req, res) {
    if(req.params.label && req.params.password){
     // do stuff
     }
}

But I want to do this more quickly. I'm wondering if there is...

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

st. skip 'path' part of URI if it's already provided in request

my st instance configured with

var mount = st({ 
  path: './app/',
  url: '/', // defaults to path option
  index: 'index.html',
  cache: {
    fd: none,
    content: none
  }
});

so when I send request to res/img/1.jpg it goes to...

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

URL component encoding in Node.js

I want to send http request using node.js. I do:

http = require('http');

var options = {
    host: 'www.mainsms.ru',
    path: '/api/mainsms/message/send?project='+project+'&sender='+sender+'&message='+message+'&recipients='+from+'&sign='+sign
   ...

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

coffee util when require(./module) takes .js file instead of .coffee

I use CoffeeScript 1.6.3. While developing, I just run with coffee myapp.

I also run coffee -c . to examine resulted .js files.

However, when running coffee myapp again, coffee util for require(./module) takes .js file instead of .coffee. While...

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

Node.js Property Traversal Order

In Node.js what is the order when iteration over properties of an object does it iterate over inherited properties before it's own properties or the other way round?

Also is this likely to change in the future aka do I need to write some feature...

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

Sequelize associations hasOne, belongsTo

The problem is that I can not get working the relation hasOne, which does not eager load the state type object.

All the queries are done on existing tables.

Here is the customer table, whats important is the cst_state_type field:

module.exports =...

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

Routing security flaw in Angular/MEAN.io?

I just installed the MEAN stack (MongoDB, Express.js, AngularJS, Node.js) and opened up the example program (as found on mean.io) and they have a basic app that you can login to and create blog "articles" just for testing and such.

Anyway, I removed...

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

How can I make AJAX requests using the Express framework?

I want to send AJAX requests using Express. I am running code that looks like the following:

var express = require('express');
var app = express();

app.get('/', function(req, res) {
   // here I would like to make an external
   // request to...

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

beginner node.js callback example

I'm pretty novice in nodejs This is a very easy php example that I want to write in nodejs

$key='foo';
$inside= openthedoor($key);
if(!$inside){ //wrong key
   $key= getanewkey();//get a new key
   $inside= openthedoor($key);//open the door...

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

dynamodb node aws-sdk simple getItem() call

Folks, New to Javascript... trying to do simple dynamo queries from node:

var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});
var db = new AWS.DynamoDB();

var params = {
"TableName" : 'admins',
"Key" : [
      { "username" : {...

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

How to access specific request parameter

I thought the format for Twilio's request parameters were being returned as JSON-serialized data --according to the documentation listed for The Response Callback section (http://twilio.github.io/twilio-node/#restResources)

If that is the case I...

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

Does NPM load certain modules after it is installed (for its own use)?

I had some issues with NPM so I decided to simply uninstall Node, NPM & NVM and then reinstall everything on Mac OS X 10.8. After following various guides and Stackoverflow questions I was pretty sure I had gotten rid of everything. But to my surprise...

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

Facebook OAuth2 does not provide user email

This has been asked many times but it seems like there's no known work-around for it so I'm posting this question in the hope that someone does have a work-around for it.

I'm using NodeJS, PassportJS-Facebook.

app.get("/auth/facebook",
           ...

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

View templates and routes in node.js with AngularJS

Trying to understand how to implement AngularJS in a node.js express app. After setting up express, I need 2 things: routing and a template engine, so normally I would need to do as follows to set the app to use Jade templating engine:

...

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

Express can't find my node modules after update to 3.x

I used to have the following piece of code in my Jade...

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

JavaScript: Callback which doesn't stop execution of lines below

Before this question, I already thought that I had mastered JavaScript.. but no, not even close :)

I'm currently programming with require.js and trying to make methods with callbacks (because of async js) but the problem is that code continues...

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

call back on cheerio node.js

I'm trying to write a scrapper using 'request' and 'cheerio'. I have an array of 100 urls. I'm looping over the array and using 'request' on each url and then doing cheerio.load(body). If I increase i above 3 (i.e. change it to i < 3 for testing) the...

Read the entire recipe Recipe added 1 day ago