【发布时间】:2020-09-13 03:08:42
【问题描述】:
我已经尝试了一切,我不能再浪费时间了,请需要帮助,我一直在尝试运行这篇文章中的代码:https://medium.com/@mogold/nodejs-socket-io-express-multiple-modules-13f9f7daed4c,我一直在尝试运行代码这篇文章,我非常喜欢它,因为我认为它非常适合大型项目,当我想连接它时出现问题,向我抛出以下错误: 控制台: 我已经尝试设置在我的应用程序的标题中,我尝试了从类似问题中读取的代码,但没有一个有效,我将 server.ts、socket.ts、job.ts 和 routes.ts 的条件留给你,我希望你能请帮帮我: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
};
客户端索引.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>
从“../features/Appointment/routes/index”导入indexAppointment;
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;
从“../features/User/routes/index”导入 indexUser;
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;
【问题讨论】:
-
您似乎没有任何端点可以从定义的前端命中。你能分享 indexAppointment 和 indexUser 的内容吗?另外 /socket.io 不应该是前端命中的内容,您可以将自定义路径传递给套接字连接以进行测试。您将需要在代码中定义该子路径。 socket.io/docs/client-api 看自定义路径。
-
我刚刚更新了它,实际上对它们进行了索引,我用来将逻辑与用户路径和约会分开
-
decasualidad hablas español ? :c
标签: node.js typescript express sockets socket.io