Im trying to pass socket and use them in routes files .
I'm exporting my routes.js module
module.exports = function (client) {
var express = require("express"); // call express
var router = express.Router(); // get an instance of the express Router
var admin = require("firebase-admin");
router.use(function (req, res, next) {
// do logging
client.on('save-message', function (socket) { console.log("heheyy") })
});
router
.route("/")
.get(function (req, res, err) {
... route stuff...
});
router
.route("/")
.post(function (req, res, err) {
... route stuff
});
}
//module.exports = router; **commented**
in my server.js I include routes module then I use it in socket connection function
var http = require("http");
var admin = require('firebase-admin');
var firebase = require("firebase");
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var port = process.env.app_port || 8080; // set our port
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
var routerProj = require("./routes/routes");
var server = app.listen(port);
var io = require("socket.io")(server);
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT ,DELETE');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,*");
next();
});
var config = {
.... configuration
};
firebase.initializeApp(config);
var serviceAccount = require("./ServiceAcountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://datatable-18f93.firebaseio.com"
});
app.use("/v1", routerProj, function (req) {
//Create HTTP server and listen on port 8000 for requests
});
// Print URL for accessing server
console.log("Server running at http://127.0.0.1:8080/");
io.on("connection", function (client) {
console.log("Un client est connecté !");
routerProj(client);
});
When I wrap my routes file in a module-function the routes does not work and dont fetch or insert data. so sockets also does not work.
for information sockets work in the server.js file