0

I am creating an app that allows users to upload files in their browsers to a server.

I started it by typing express in the folder of my project. With that express created all the folders and such. It created a bin folder containing a www file that starts a lot of stuff for us like the ports.

At first i only had one core so i was doing it like so:

var express = require('express');

var multer = require('multer');

var app = express();

var done = false;
var fileSize = 0;

app.use(multer({ dest: './uploads/',
    rename: function (fieldname, filename){
        return filename;
    },
    onFileUploadStart: function(file){
        console.log(file.originalname + ' is starting...');
    },
    onFileUploadComplete: function(file){
        console.log(file.fieldname + ' uploaded to ' + file.path);
        done = true;
    },
    onFileUploadData: function(file, data){
        fileSize += data.length;
        console.log("FileUpload " + fileSize);
    }
}));

app.get('/', function(request, response){
    response.sendFile(__dirname + '/index.html');
});

app.post('/upload/', function(request, response){
    console.log(request.body);
    if (done == true)
        response.end('file uploaded');
});
module.exports = app;

Eventually i needed to user a machine with several cores in order to be able to respond to more client requests. So I am trying to use the cluster module.

I changed the code accordingly:

var cluster = require('cluster');

if(cluster.isMaster){
    var numCPUS = require('os').cpus().length;
    for(var i=0; i<numCPUS; i++)
        cluster.fork();

    cluster.on('exit', function(worker, code, signal) {
        console.log('worker ' + worker.process.pid + ' died');
    });

    cluster.on('online', function(worker) {
        console.log('worker ' + worker.process.pid + ' started');
    });

}else{
    var express = require('express');
    var multer = require('multer');

    var app = express();

    var done = false;
    var fileSize = 0;

    app.use(multer({ dest: './uploads/',
        rename: function (fieldname, filename){
            return filename;
        },
        onFileUploadStart: function(file){
            console.log(file.originalname + ' is starting...');
        },
        onFileUploadComplete: function(file){
            console.log(file.fieldname + ' uploaded to ' + file.path);
            done = true;
        },
        onFileUploadData: function(file, data){
            fileSize += data.length;
            console.log("FileUpload " + fileSize);
        }
    }));

    app.get('/', function(request, response){
        response.sendFile(__dirname + '/index.html');
    });

    app.post('/upload/', function(request, response){
        console.log(request.body);
        if (done == true)
            response.end('file uploaded');
    });
    module.exports = app;
}

The problem is that when i do this i am getting the following error in the console:

Run: node ./bin/www Result:

app.set('port', port);

TypeERror: Cannot call method 'set' of undefined at Object.<anonymous> 

This happens since i placed the entire code inside the forked childs of the master. Anyone knows why this happens and how i can fix it?

EDIT: In here you can see the bin/www file where the port and some other configurations are set:

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('dropbox:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}
Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
  • 1
    You seem to be posting unrelated code, somehere there's a server being set up, with `listen` etc. and `app.set('port')`, and that part isn't included in the code above – adeneo Feb 11 '15 at 13:00
  • Yes it is set in the bin folder in the www file. which i run and is able to access this code with the module.exports = app. i will post it. – JoaoFilipeClementeMartins Feb 11 '15 at 13:46
  • It has to do with the fact that module.exports = app; is inside the else statement – JoaoFilipeClementeMartins Feb 11 '15 at 13:48
  • `app` is clearly not defined in the file that sets the port – adeneo Feb 11 '15 at 14:14
  • @adeneo It is not it is in another file. But when i remove the code for the cluster everything works fine. – JoaoFilipeClementeMartins Feb 11 '15 at 14:25
  • Wherever you're calling `app.port()`, it's clear that `app` is not defined. Look at where `var app = express();` is set, and try to figure out why `app` isn't available where the port is being set, it's not very clear from the posted code how this all works together. – adeneo Feb 11 '15 at 14:29

0 Answers0