0
votes

This is My Chat Application file code

var worker=function(worker) {
var http=require('http');
var fs=require('fs');
var app=http.createServer(function(request,response){

fs.readFile("index.html",'utf-8',function(err,data){
response.writeHead(200, {'Content-Type': 'text/html'});
    response.write(data);
    response.end();


});
}).listen(8080);


var io = require('socket.io').listen(app);

io.sockets.on('connection', function(socket) {
socket.on('message_to_server', function(data) {
    io.sockets.emit("message_to_client",{ message: data["message"] });
  });
});
};
 module.exports=worker;

This is My server.js File

  var express  = require('express');
  var io = require('socket.io');
   var worker = require("./chat/main.js");
    var app      = express();
  var port     = process.env.PORT || 8080;
 var mongoose = require('mongoose');
 var passport = require('passport');
  var flash      = require('connect-flash');

  var configDB = require('./config/database.js');


   mongoose.connect(configDB.url);

  require('./config/passport')(passport); 

  app.configure(function() {


app.use(express.logger('dev')); 
app.use(express.cookieParser()); 
app.use(express.bodyParser()); 

app.set('view engine', 'ejs'); 


app.use(express.session({ secret: 'ilovescotchscotchyscotchscotch' })); 
app.use(passport.initialize());
app.use(passport.session()); 
app.use(flash()); 

    });


 require('./app/routes.js')(app, passport,worker);

 app.listen(port);



  console.log('The magic happens on port ' + port);

And in Route.js File

I am writing this code

 module.exports = function(app, passport,worker) {
 app.get('/chat', function(req, res) {
    req.worker();
    res.redirect('/chat');
});

After running the code i am not able to redirect to chat page

error coming-

TypeError: Object # has no method 'worker' at C:\Users\Sanjay1\Desktop\fmodule\login\app\routes.js:11:7 at callbacks (C:\Users\Sanjay1\Desktop\node_modules\express\lib\router\index.js:164:37) at param (C:\Users\Sanjay1\Desktop\node_modules\express\lib\router\index.js:138:11) at pass (C:\Users\Sanjay1\Desktop\node_modules\express\lib\router\index.js:145:5) at Router._dispatch (C:\Users\Sanjay1\Desktop\node_modules\express\lib\router\index.js:173:5) at Object.router (C:\Users\Sanjay1\Desktop\node_modules\express\lib\router\index.js:33:10) at next (C:\Users\Sanjay1\Desktop\node_modules\express\node_modules\connect\lib\proto.js:193:15) at Object.handle (C:\Users\Sanjay1\Desktop\node_modules\connect-flash\lib\flash.js:21:5) at next (C:\Users\Sanjay1\Desktop\node_modules\express\node_modules\connect\lib\proto.js:193:15) at SessionStrategy.strategy.pass (C:\Users\Sanjay1\Desktop\node_modules\passport\lib\middleware\authenticate.js:314:9)

1

1 Answers

0
votes

You are not defined req.worker() method. To use socket.io and express in one file look at example:

/**
 * Module dependencies.
 */

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

var app = express();
var server = app.listen(3000);

// Tell socket.io to work on the same port with express server
var io = require('socket.io').listen(server);

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.static(__dirname + '/public'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
});

app.get('/', routes.index);

io.sockets.on('connection', function(socket) {
    socket.on('message_to_server', function(data) {
        io.sockets.emit("message_to_client",{ message: data["message"] });
    });
});

Example got there.