如何在nodejs中的一个文件中组合两个应用程序

时间:2014-05-14 20:37:22

标签: javascript node.js express

这是我的聊天应用程序文件代码

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;

这是我的server.js文件

  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);

在Route.js文件中

我正在写这段代码

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

运行代码后,我无法重定向到聊天页面

错误来了 -

TypeError:对象#在C:\ Users \ Sanjay1 \ Desktop \ fmodule \ login \ app \ routes.js:11:7回调时没有方法'worker'(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 \在Router._dispatch上的Sanjay1 \ Desktop \ node_modules \ express \ lib \ router \ index.js:145:5)(C:\ Users \ Sanjay1 \ Desktop \ node_modules \ express \ lib \ router \ index.js:173:5)在Object.router(C:\ Users \ Sanjay1 \ Desktop \ node_modules \ express \ lib \ router \ index.js:33:10)下一步(C:\ Users \ Sanjay1 \ Desktop \ node_modules \ express \ node_modules \ connect \ lib \ proto.js:193:15)在Object.handle(C:\ Users \ Sanjay1 \ Desktop \ node_modules \ connect-flash \ lib \ flash.js:21:5)下一步(C:\ Users \ Sanjay1 \ Desktop \ node_modules \ express \ node_modules \ connect \ lib \ proto.js:193:15)在SessionStrategy.strategy.pass(C:\ Users \ Sanjay1 \ Desktop \ node_modules \ passport \ lib \ middleware \ authenticate.js:314: 9)

1 个答案:

答案 0 :(得分:0)

您没有定义req.worker()方法。要在一个文件中使用socket.io和express,请查看示例:

/**
 * 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"] });
    });
});

示例got there