raficsedu raficsedu - 6 months ago 209
Node.js Question

Node js app.configure is not a function

Can anyone tell what is wrong within this code .I have installed all necessary modules .When i run this script then it tells app.configure is not a function . If i am missing any thing , please suggest me .

var express = require('express')
, app = express()
, server = require('http').createServer(app)
, io = require("socket.io").listen(server)
, npid = require("npid")
, uuid = require('node-uuid')
, Room = require('./room.js')
, _ = require('underscore')._;

app.configure(function() {
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 3000);
app.set('ipaddr', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.use('/components', express.static(__dirname + '/components'));
app.use('/js', express.static(__dirname + '/js'));
app.use('/icons', express.static(__dirname + '/icons'));
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);

/* Store process-id (as priviledged user) */
try {
npid.create('/var/run/advanced-chat.pid', true);
} catch (err) {
console.log(err);
//process.exit(1);
}

});

Answer

app.configure() is no longer part of Express 4.

See here: https://github.com/expressjs/express/wiki/Migrating-from-3.x-to-4.x

You no longer need to use app.configure and instead can directly configure your express app like so.

var express = require('express')
, app = express()
, server = require('http').createServer(app)
, io = require("socket.io").listen(server)
, npid = require("npid")
, uuid = require('node-uuid')
, Room = require('./room.js')
, _ = require('underscore')._;


app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 3000);
app.set('ipaddr', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.use('/components', express.static(__dirname + '/components'));
app.use('/js', express.static(__dirname + '/js'));
app.use('/icons', express.static(__dirname + '/icons'));
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);

/* Store process-id (as priviledged user) */
try {
    npid.create('/var/run/advanced-chat.pid', true);
} catch (err) {
    console.log(err);
    //process.exit(1);
}

If you need environment specific configurations, use an if block:

if(process.env.NODE_ENV === 'production') {
  app.set('port', 80);
  // additional prod environemtn configuration
}