Answer the question
In order to leave comments, you need to log in
Socket.io + Express + API?
The problem is how to make it possible to send socket.emit inside a specific file-splitting route.
main server js
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io').listen(config.portSockets);
// всякое
require('routes')(app, io);
// всякое
// дополнительные зависимости
module.exports = function (app, io) {
// допустим роут с пользователями
require('./user')(app, io);
// еще код
}
module.exports = function (app, io) {
app.get(prefix + 'user/:id',
require('routes/user/getById').get);
}
exports.get = function (req, res, next) {
// Итак как мне сюда передать ссылку на переменную io, чтобы вызвать ее по необходимости???
};
Answer the question
In order to leave comments, you need to log in
As it always turned out, everything is simpler, we write middleware, where we embed our io into the res object
app.use(function(req, res, next){
res.io = io;
next();
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question