1

My application folder structure is something like this.

app/ config/ app.js env.js server.js

Whenever I start my run my app.js file it gives me server started at undefined. Here is the gist of the code.

Gist Codes

server.js    
// Module dependencies.
var http = require('http'),
    app = require('./config/app')();

// Start server
app.listen(app.get('port'), function(){
    console.log('App is Up at '+ app.get('port') + ' as ' + process.env.NODE_ENV);
});

config/app.js
var express  =  require('express'),
    path  =  require('path'),
    ejs  =  require('ejs');

module.exports = function() {
    var app = express();
    var publicDir = path.join(__dirname, '../public');

    // Store all environment variables
    app.set('port', process.env.PORT || 3000);

    // Basic configuration
    app.configure(function() {
        app.use(express.logger('dev'));
        app.use(express.bodyParser());
        app.use(express.methodOverride());
        app.use(app.router);
        app.use(express.static(publicDir));
    });

    // Environment specific configuration
    require('./env')(app);

    return app;
};
config/env.js
var express = require('express'),
    hbsPrecompiler = require('handlebars-precompiler'),
    path = require('path');

module.exports = function(app) {
    // development compile Handlebars and show errors
    app.configure('development', function(){
        app.set('db-uri', process.env.MONGOLAB_URI || 'mongodb://localhost/App');

        hbsPrecompiler.watchDir(
            path.join(__dirname, "../public/templates"),
            path.join(__dirname, "../public/templates/compiled/templates.js"),
            ['handlebars', 'hbs']
        );
        app.use(express.errorHandler());
    });

    app.configure('production', function(){
        app.set('db-uri', process.env.MONGOLAB_URI);
    });
};

node server.js

output

/usr/bin/node app.js
App is Up at 3000 as undefined
0

3 Answers 3

2

By default, process.env.NODE_ENV is undefined. If you run node in the test environment, for instance (NODE_ENV=test node app.js) you wouldn't have that problem.

I suggest you add this line in your module.exports function:

if (app.settings.env === 'development') process.env.NODE_ENV = 'development';
Sign up to request clarification or add additional context in comments.

3 Comments

It is working, one question though , if I put this in the config/app.js module.exports, is it going to get all the dev requirements that is specified in the config/env.development ?
Yes. If you add that line at the beginning of your config/env.js module.exports function, then your app will be configured as specified under your app.configure('development'... block.
i think it should go in the config/app.js rather than env.js cause again i have to initialize the application in the env file. Update: Nopes it should be in the env file.
0

This looks different than the folder structure you mentioned:

app = require('./config/app')();

It seems reversed.

1 Comment

first app.js is the root file. lemme change that to server.js
0

This is as expected. There is no NODE_ENV environment variable by default unless you explicitly set it, so process.env.NODE_ENV is undefined. There's no problem with that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.