V
V
Vladislav Yandimirkin2016-04-01 17:32:50
Node.js
Vladislav Yandimirkin, 2016-04-01 17:32:50

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);
// всякое

here route/index.js
// дополнительные зависимости
module.exports = function (app, io) {
    // допустим роут с пользователями
    require('./user')(app, io);
   // еще код
}

user/index.js
module.exports = function (app, io) {
app.get(prefix + 'user/:id',
        require('routes/user/getById').get);
}

user/getById.js module
exports.get = function (req, res, next) {
   // Итак как мне сюда передать ссылку на переменную io, чтобы вызвать ее по необходимости???
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Yandimirkin, 2016-04-01
@vlaad360

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 question

Ask a Question

731 491 924 answers to any question