Hi all, asking this because I've been dealing with this issue all day. Basically, I have an app.ts file where I set up all of the configuration for my app and connect to my db. I do the following:export const app = express(); I end up importing this into server.ts which is where I actually create the server and start the listening:import * as http from "http"; import { app } from "./app"; export const server = http.createServer(app); server.listen(app.get("port"), () => { console.log("App is running at http://localhost:%d in %s mode", app.get("port"), app.get("env")); }); Now, you might be asking why export the server? This is because I am also making a WebSocket server in one of my routes (called conversationController.ts). I don't know if that is best practice, but I didn't want my regular server in the same file as my WebSocket server (because that would require having all of my WebSocket event handling logic into my server.ts, no?).Anyway, in conversationController.ts, I do the following:imports .... import { server } from "../server"; const wss = new WebSocket.Server({ server }); I thought this would work; I didn't even get any errors when I wrote it. However, when I compile it, I get the following error:TypeError: missing or invalid options I ended up figuring out about cyclic dependencies and such. Basically, at the time of constructing the WebSocket Server using the imported server, the imported server is undefined, which is why the error is occurring.So, I'd like to know; am I going about this the right way? Or, do I HAVE to have my regular server and WebSocket server defined in the same file? (This would obviously solve the issue of the undefined server that is passed to the WebSocket constructor). But like I said, the reason that I am separating the servers into different files is because I'd like to have all of the logic for handling the WebSocket events in its own controller, and regular server logic (serving static files, for example) in its own file. It would be really sucky to have logic that deals with only one specific subroute forced to be in my server.ts.I guess this really boils down to: how can I seperate the logic for my "regular" server and the logic for my WebSocket server into different files (basically into src/server.ts and src/controllers/conversationController.ts, respectively). I'm actually very surprised that there aren't posts on SO tackling this same problem; I'm thinking I'm just not doing it right.Would really love some advice on this. Thanks.
Submitted January 05, 2018 at 06:32AM by HeroicSpiritNick
No comments:
Post a Comment