Answer the question
In order to leave comments, you need to log in
How to correctly build an algorithm for interactions between modules in Node.js?
Hello.
I work in WebStorm.
I am making a website using the express package and socket.io.
This is how the basic configuration of an ordinary blank page of my site looks like:
var express = require('express');
var network = require('../lib/socket.js');
var users = require('../lib/users.js');
var log = require('../lib/log.js');
var router = express.Router();
router.get('/', function(req, res, next) {
if(req.user !== undefined) {
var token = req.cookies.token;
log.Debug("[LawsPage] Connected token: {0} ", [token]);
users.getDBUserByToken(token, function (user) {
if(user === undefined) {
res.redirect('/');
} else {
res.render('laws', {
username: user.name,
useravatar: user.avatar,
userbalance: user.balance
});
}
});
} else {
res.redirect('/');
}
});
network.io.on('connection', function (socket) {
var token = socket.handshake.query.token;
users.onConnectSocket(socket, token);
log.Debug("[Laws -> Socket.IO] New user connected! Token: {0} Socket ID: {1}", [token, socket.id]);
socket.on('disconnect', function() {
users.onDisconnectSocket(socket);
log.Debug("[Socket.IO] User disconnected! Token: {0} Socket ID: {1}", [token, socket.id]);
});
});
module.exports = router;
var io = require('socket.io');
//Инициализация экспрессом
module.exports = function(server) {
var listener = io.listen(server);
console.log("[Socket.IO] Listening...");
module.exports.io = listener;
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question