TypeError:object不是Node JS / socket.io中的函数

时间:2016-08-30 06:53:32

标签: javascript node.js socket.io mean-stack

我对MEAN堆栈很新。启动我的服务器>我正面临“TypeError:object is not function” node index.js。

.../master/server/app.js:47
require('./config/socketio')(socketio);
                            ^
TypeError: object is not a function
    at Object.<anonymous> (/home/divine20/master/server/app.js:30:2)
    at Module._compile (module.js:456:26)
    at loader (/home/divine20/master/node_modules/babel-core/node_modules/babel-register/lib/node.js:146:5)
    at Object.require.extensions.(anonymous function) [as .js] (/home/divine20/master/node_modules/babel-core/node_modules/babel-register/lib/node.js:156:7)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/divine20/master/server/index.js:12:28)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

我的Index.js文件是

'use strict';

    // Set default node environment to development
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';

if (env === 'development' || env === 'test') {
  // Register the Babel require hook
  require('babel-core/register');
}

// Export the application
exports = module.exports = require('./app');

App.js文件

/ **  *主要申请文件  * /

'use strict';

import express from 'express';
import mongoose from 'mongoose';
mongoose.Promise = require('bluebird');
import config from './config/environment';
import http from 'http';

// Connect to MongoDB
mongoose.connect(config.mongo.uri, config.mongo.options);
mongoose.connection.on('error', function(err) {
  console.error('MongoDB connection error: ' + err);
  process.exit(-1);
});

// Populate databases with sample data
if (config.seedDB) { require('./config/seed'); }

// Setup server
var app = express();
var server = http.createServer(app);
 var socketio = require('socket.io')(server, {
   serveClient: config.env !== 'production',
   path: '/socket.io-client'    
 });
 require('./config/socketio')(socketio);
 require('./config/express')(app);
 require('./routes')(app);

// Start server
function startServer() {
  app.angularFullstack = server.listen(config.port, config.ip, function() {
    console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
  });
}

setImmediate(startServer);

// Expose app
exports = module.exports = app;

// Socketio.js

/**
 * Socket.io configuration
 */
'use strict';

import config from './environment';

// When the user disconnects.. perform this
function onDisconnect(socket) {
}

// When the user connects.. perform this
function onConnect(socket) {
  // When the client emits 'info', this listens and executes
  socket.on('info', data => {
    socket.log(JSON.stringify(data, null, 2));
  });

  // Insert sockets below
  require('../api/utility/utility.socket').register(socket);
  require('../api/repayment/repayment.socket').register(socket);
  require('../api/transaction/transaction.socket').register(socket);
  require('../api/offer/offer.socket').register(socket);
  require('../api/payment/payment.socket').register(socket);
  require('../api/yelp/yelp.socket').register(socket);
  require('../api/faq/faq.socket').register(socket);
  require('../api/blog/blog.socket').register(socket);
  require('../api/user/user.socket').register(socket);
  //require('../api/listing/listing.socket').register(socket);
}

export default function(socketio) {
  // socket.io (v1.x.x) is powered by debug.
  // In order to see all the debug output, set DEBUG (in server/config/local.env.js) to including the desired scope.
  //
  // ex: DEBUG: "http*,socket.io:socket"

  // We can authenticate socket.io users and access their token through socket.decoded_token
  //
  // 1. You will need to send the token in `client/components/socket/socket.service.js`
  //
  // 2. Require authentication here:
  // socketio.use(require('socketio-jwt').authorize({
  //   secret: config.secrets.session,
  //   handshake: true
  // }));

  socketio.on('connection', function(socket) {
    socket.setMaxListeners(20);
    socket.address = socket.request.connection.remoteAddress +
      ':' + socket.request.connection.remotePort;

    socket.connectedAt = new Date();

    socket.log = function(...data) {
      console.log(`SocketIO ${socket.nsp.name} [${socket.address}]`, ...data);
    };

    // Call onDisconnect.
    socket.on('disconnect', () => {
      onDisconnect(socket);
      socket.log('DISCONNECTED');
    });

    // Call onConnect.
    onConnect(socket);
    socket.log('CONNECTED');
  });
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

您正在使用默认导出。您用来编译代码的任何内容(例如Babel或TypeScript)都可能通过在名为module.exports的{​​{1}}上添加属性来发出默认导出。

尝试写写default