0x_Anakin
0x_Anakin

Reputation: 3269

node.js express app jade templates not rendering

I'm trying to create a cluster server with socket.io and express.js I'm following various tutorials on the internet as well on youtube. What I have at the moment is this code in my app.js:

var cluster = require('cluster');

if (cluster.isMaster) {

    var cpuCount = require('os').cpus().length;
    var workers = [];   

    for (var i = 0; i < cpuCount; i++) {
        workers[i] = cluster.fork();
    }

    cluster.on('exit', function (worker){

        for (var i = 0; i < workers.length; i++) { 
            if (worker.process.pid === workers[i].process.pid) {
                workers.splice(i, 1);
            }
        }

        for (var i = 0; i < cpuCount - workers.length; i++) {
            workers.push(cluster.fork());
        } 

    });

} else {

    /**
     * Module dependencies.
     */

    var express = require('express');
    var routes = require('./routes');
    var http = require('http');
    var path = require('path');

    var app = express();

    app.set('port', process.env.PORT || 3000);
    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);

    var server = http.createServer(app);
    var io = require('socket.io').listen(app.get('port'));

}

When I go to http://localhost:3000/ I get the response:

Welcome to socket.io.

In my previous test scripts I didnt have this issue and my jade templates were being rendered fine. Could someone explain why is this happenning?

Furthermore in my routes directory I have the script: index.js with this code:

/*
 * GET home page.
 */

exports.index = function(req, res){
  res.render('index', { title: 'Express' });
};

Finally in my views folder I have layout.jade with:

doctype 5
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
    block content

and index.jade with:

extends layout

block content
  h1= title
  p Welcome to #{title}

Upvotes: 1

Views: 472

Answers (1)

0x_Anakin
0x_Anakin

Reputation: 3269

It seems that the problem was in the final lines of app.js

this fixes the issue:

var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(app.get('port'));

Sorry for any inconvenience.

Upvotes: 1

Related Questions