Answer the question
In order to leave comments, you need to log in
How to export socket.io module for use in other files (routes)?
How to export socket.io module for use in other files (routes)?
App.js
module export examplemysql
// mysql
const db = require('./db');
let sql = `SELECT * FROM users WHERE login LIKE '${login}'`;
db.query(sql, (err, result) => {
if (err) throw err;
if (result) {
console.log(result);
res.json({
ok: true
});
}
});
const mysql = require('mysql');
// Connect to DB
const db = mysql.createConnection({
host: "localhost",
user: "root",
database: "mytable",
password: "",
multipleStatements: true
});
db.connect(function(err) {
if (err) throw err;
console.log("MySQL Connected!");
});
module.exports = db;
const io = new Server(server);
module.exports = io;
const io = require('/routes/app');
io.on('connection', (socket) => {
socket.on('chat message', msg => {
io.emit('chat message', msg);
});
});
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