Latest Node.js Recipes

How to clean all sessions in Nodejs (heroku + redis)?

I'm using nodejs (expressjs) hosted on heroku.

Sessions are stored in redis (Redistogo plugin for heroku):

RedisStore = require('connect-redis')(express)
app.use express.session
      secret: process.env.CLIENT_SECRET
      cookie: { maxAge:...

Read the entire recipe Recipe added 8 minutes ago

Thumbnails from graphics magick without upscaling

I am using the graphics magick wrapper in nodejs and am creating square thumbnails using the following code:

var size = {width: 200, height: 200};
gm(sourcePath)
  .resize(size.width * 2, (size.height * 2) + '')
  .thumbnail(size.width, size.height +...

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

Javascript regex (extract string)

I always forget how to create simple regular expressions despite doing projects with more complex regexes...

I have the string:

var s = "qwehref=\"1232.css\"qwasd asd asdqq eehref=\"asd.css\""

I want to capture the text inside the href tags:...

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

node.js Buffer binary output is different

This has me very confused. I have the following socket server (simplified) in node.js:

net.createServer(function (socket) {
    socket.on('data', function (data) {
        var replyData = new Buffer('78780E00C425BA53269830303000006C2D0D0A',
         ...

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

Node.js + AngularJS + socket.io: connect state and manually disconnect

I use nodejs with socket.io and angularjs on client. I picked up angular-socketio example from the Internet and added disconnect method to It.

Socket service:

angular.module('app')
  .factory('socket', ['$rootScope', function ($rootScope) {

    var...

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

Node.js Sequelize ManyToMany relations producing incorrect SQL

I'm having a problem with sequelize ManyToMany relations.

Here are my models...

var db = {

    players: sequelize.define('players', {
        name: Sequelize.STRING
    }),

    teams: sequelize.define('teams', {
        name: Sequelize.STRING
   ...

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

"Unexpected end of input" message on response from simple POST request to node.js

I've written a simple node.js program to demonstrate the problem I'm running into elsewhere.

Given the following node.js program:

var http = require('http');
http.createServer(function (req, res) {
// simple repro for json deserializaiton error...

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

Write file to a subdirectory node.js

I am working with node and express and i have a directory structure like

public
   img
   css
   js
server.js

in server.js i have the following code for writing to a png file

fs.writeFile('testfile.png', imageData, function(err){

       ...

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

Post a base64 image to Imgur

http.get('http://path/to/image.jpg', function (res) {
  var img = '';
  res.on('data', function (buff) {
    img += buff;
  });
  res.on('end', function () {
    var data = querystring.stringify({
      image: img.toString('base64'),
      type:...

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

How can I change my nodejs program to node?

I downloaded NodeJS what I thought was the normal way, but for some reason after installing it on my linux machine, the nodeJS program is called "nodejs" rather than "node". This is problematic because every module I get from NPM uses "node" to run...

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

node-postgres returning multiple array_length() columns?

I have 3 array columns and i am trying to get the count for each of these arrays but node-postgres is only returning one of them which is the last array_length() passed in the SQL statement. I passed the same sql query using the postgres console and...

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

Set options to array field in Mongoose schema

How can I pass options such as required: true or default to an Array field?

See the documentation here, it only shows examples like this one:

ArrayOfString: [String]

But I want to be able to do something like this:

ArrayOfString: { type:...

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

Node.js: npm doesn't install anymore

I don't know what I did wrong, but trying to install a module using npm returns errors. My concrete example (same results, no matter which module I would like to install):

npm install socket.io

Returns:

npm ERR! TypeError: Cannot call method...

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

Inconsistent behavior with HTTP POST requests in Python

Trying to make a POST request between a Python (WSGI) and a NodeJS + Express application. They are on different servers.

The problem is that when using different IP addresses (i.e. private network vs. public network), a urllib2 request on the...

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

Is it bad practice to to use wrapping anonymous function for module.exports in Node?

I've started to use wrapping anonymous function in my NodeJS express modules lately as it somehow helps me write cleaner code.

However I'm not sure if this is considered bad/good practice in the Node environment (maybe some debugging/optimization...

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

Change Model values after load in Mongoose

In my mongoose model, I have some stats that are dependent on time. My idea is to add a middleware to change these stats right after the model has been loaded.

Unfortunately, the documentation on the post-Hooks is a bit lacking in clarity. It seems...

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

running node.js server using upstart causes 'terminated with status 127' on 'ubuntu 10.04'

i have written an upstart script for ubuntu to launch my node.js server manually or on startup. But it always terminates with status 127 and i can't find more information about what is going wrong. If i execute it manually then it works and i also...

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

Ensure the most accurate synchronization

First I'll explain my problem : I've got a node.js server and a lot of JS client that receive events through socket.io. A web application send a end date (timestamp) to the node.js server and node.js broadcast this timestamp to all clients. Then...

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

Node.js Socket.io, socket.emit doesn't work

I'm pretty new here. I'm trying to use the emit function in this case with no luck :(.

Server Side:

socket.on('newUser', function(msg){
    ConnectedSockets ++;
    var player;
    player = new Player();

    player.CreatePlayer(msg.id, msg.name,...

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

how can superagent and nock work together?

In node.js, I have trouble making superagent and nock work together. If I use request instead of superagent, it works perfectly.

Here is a simple example where superagent fails to report the mocked data:

var agent = require('superagent');
var nock =...

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

cost of passing parameter in JavaScript(node.js)

I am creating a node.js based website where I ask users to fill forms with large number of details, so each one of them becomes a parameter.

The way I have written the code is that I keep passing these parameters from one function to another. I think...

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

Node js - user auth, what to store in session?

First of all, I did not find any similar questions or material that will be useful. I'm sorry if I missed something!

Second of all, I'm not interested in ready solutions like node-passport or connect-auth or everyauth

Now to the question. I'm using...

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

Subfolder views expressjs 3x /nodejs

How can I use a subfolder for my 'views'? In the main 'app.js' I've set the root folder for the views like so:

 app.configure(function(){
       app.set('view engine', 'jade');
       app.set('views', __dirname + '/apps' );
      ...

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

Nginx + (nodejs, socketio, express) + php site

I'm working on a fully js, HTML5 canvas game and want it to be 'real-time'. Based on my research I find out node.js is an exciting prospect, so I configured it on my ubuntu 12 webserver with socket.io, express etc.

I'm a programmer, but just a rookie...

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

Meteor - Check if user is logged in as administrator (Client - Side)

I'm currently developing an app which needs users and administrators. What I do right now is, I create an admin account on the client with username 'admin' and a default password that should be changed over the accounts-ui.

I do this because creating...

Read the entire recipe Recipe added 1 day ago