I've tried everything and I can't afford to waste any more time please need help, I've been trying to run the code in this post:https://medium.com/@mogold/nodejs-socket-io-express-multiple-modules-13f9f7daed4c,I've been trying to run the code in this post, I liked it a lot because as I see it fits very well with big projects, the problem arises when I want to connect to it, throws the following errors at me:
console:
I already try setting up the headers of my application and I've tried codes that I read from similar problems but none ah worked, I leave you the condigo of my server.ts, socket.ts, job.ts and routes.ts, I hope you can help me please :c
server.ts
import express from "express";
const http = require("http");
import { router} from './routes/routes';
import bodyParser from "body-parser";
import morgan from "morgan";
import { PORT} from "./core/utils/config"
import errorMiddleware from './core/middleware/error.middleware';
import socket from "./socket";
const app = express();
const server = http.createServer(app);
app.use(bodyParser.json());
app.use(router);
app.use(morgan("dev"));
app.use(errorMiddleware);
app.listen(3000, ()=> console.log("[SERVER] list is running in port http://localhost:"+PORT));
socket.connect(server);
socket.ts
let connection: any = null;
export class Socket {
socket: any;
constructor() {
this.socket = null;
}
connect(server: any) {
const io = require("socket.io").listen(server);
io.on("connection", (socket: any) => {
this.socket = socket;
});
}
emit(event: any, data: any) {
this.socket.emit(event, data);
}
static init(server: any) {
if (!connection) {
connection = new Socket();
connection.connect(server);
}
}
static getConnection() {
if (connection) {
return connection;
}
}
}
export default {
connect: Socket.init,
connection: Socket.getConnection
}
job.ts
import socket from "../../../socket";
export class JobSockets {
emitUpdate() {
const connection = socket.connection();
if (connection) {
connection.emit("jobs", {
hola: "hola desde mi backend"
});
}
}
}
routes.ts
import express from "express";
import indexAppointment from "../features/Appointment/routes/index";
import indexUser from "../features/User/routes/index";
import cors from "cors";
const router = express.Router();
const options: cors.CorsOptions = {
allowedHeaders: [
'Origin',
'X-Requested-With',
'Content-Type',
'Accept',
'X-Access-Token',
],
credentials: true,
methods: 'GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE',
origin: "*",
preflightContinue: false,
};
router.use(cors(options));
router.options('*', cors(options));
router.use(indexAppointment);
router.use(indexUser);
export {
router
};
client index.html
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
</head>
<body>
<p>Check the console to see the messages coming through</p>
<script>
let socket;
window.onload = () => {
socket = io.connect("http://192.168.1.7:3000");
socket.on("jobs", (msg) => {
console.log(msg)
})
};
</script>
</body>
</html>
import indexAppointment from "../features/Appointment/routes/index";
import expres from "express"
import getAppointment from "./getAppointment/getAppointment";
import createAppoinment from "./createAppointment/create_appointment";
import updateAppoinment from "./updateAppointment/update_appointment";
import deleteAppoinment from "./deleteAppointment/delete_appointment";
const router = expres.Router();
router.use("/appointment",getAppointment);
router.use("/appointment",createAppoinment);
router.use("/appointment",updateAppoinment);
router.use("/appointment",deleteAppoinment);
export default router;
import indexUser from "../features/User/routes/index";
import expres from "express"
import createUser from "./createUser/create_user";
import deleteUser from "./deleteUser/delete_user";
import updateUser from "./updateUser/update_user";
import getUsers from "./getUser/get_user";
import createUserInfoAppointment from "./createUserInfoAppointment/create_user_info_appointment";
import getUserInfoAppointments from "./getuserinfoappointment/get_user_info_appointment";
const router = expres.Router();
router.use("/user",createUser);
router.use("/user",deleteUser);
router.use("/user",updateUser);
router.use("/user",getUsers);
//managment use case
router.use("/user",createUserInfoAppointment);
router.use("/user",getUserInfoAppointments);
export default router;
