gpt4 book ai didi

javascript - 类型错误 : object is not a function In Node JS/socket. io

转载 作者:行者123 更新时间:2023-12-03 06:13:24 26 4
gpt4 key购买 nike

我对 MEAN 堆栈非常陌生。我在启动服务器> Node index.js时遇到“TypeError:对象不是函数”。

.../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');
});
}

提前致谢。

最佳答案

您正在使用默认导出。无论您使用什么来编译代码(例如 Babel 或 TypeScript),都可能通过在名为 defaultmodule.exports 上添加属性来发出默认导出。

尝试写 require('./config/socketio').default(socketio)

关于javascript - 类型错误 : object is not a function In Node JS/socket. io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39220667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com