1

I'm trying to separate server initiation and other calls from the core file(app.js) but when I try to run it, it issues me error that http.createServer(app).listen(app.get('port'), function(){ ^ TypeError: Object function (){ all code from app.js file } has no method 'get'

This is app.js file.

/**
* Module dependencies.
*/
module.exports = function(){
    var express = require('express');
    var routes = require('./routes');
    var path = require('path');
    var app = express();

    // all environments
    app.set('port', process.env.PORT || 4000);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(path.join(__dirname, 'public')));

    // development only
    if ('development' == app.get('env')) {
        app.use(express.errorHandler());
    }
    app.get('/', routes.index);
    return app;
};

and this is server.js file.

var http = require('http'),
    app = require('./app');
http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

using express@3.4.0

what am I missing OR doing wrong.. please help.

MMK
  • 365
  • 4
  • 15

2 Answers2

3

You have no reason to return a function into your app.js file, just return the express object:

var express = require('express');
var app = express();
// ... more variables

// ... the rest of your code

module.exports = app;

Then, the rest of your code into server.js will work fine.

Remember that module.exports works like a "return" into CommonJS (and therefore NodeJS).

See documentation.

lante
  • 6,736
  • 2
  • 31
  • 56
2

You're passing a function to module.exports, so when you require('./app'), you need to call it like a function:

var http = require('http'),
    app = require('./app')();
http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
jValdron
  • 3,374
  • 1
  • 26
  • 44
  • thank, worked..and if I don't want to call it like function then what I'll have to change in`app.js`? – MMK Dec 02 '14 at 13:58
  • See @lante answer. Personally, when I separate modules, I like to have functions as it allows me to pass in arguments and use arguments in the `app.js` file. – jValdron Dec 02 '14 at 13:59
  • yes, got that,I'll also pass an argument,not now, so this approach seems to be nicer.. – MMK Dec 02 '14 at 14:00
  • I'm using it on `Linux Mint 17`. I ran this on `Windows 7` and when I try to install `connect-mongoose` it says that I should have `.Net framework 2.0` installed, but I've installed it already. any suggestion? – MMK Dec 02 '14 at 14:03
  • 1
    Sounds like a different question ;) However, not being familiar with the `connect-mongoose` package, it sounds like it's trying to compile a native module. So you'll need either Visual Studio or the Windows SDKs. See this question for more details: http://stackoverflow.com/questions/14278417/cannot-install-node-modules-that-require-compilation-on-windows-7-x64-vs2012 – jValdron Dec 02 '14 at 14:06