Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(options: ApplicationConfig = {}) {
super(options);
/**
* Create an Express app to serve the home page
*/
const expressApp = express();
const root = path.resolve(__dirname, '../../public');
expressApp.use('/', express.static(root));
// Create an http server backed by the Express app
this.httpServer = new HttpServer(expressApp, options.websocket);
// Create ws server from the http server
const wsServer = new WebSocketServer(this.httpServer);
this.bind('servers.websocket.server1').to(wsServer);
wsServer.use((socket, next) => {
console.log('Global middleware - socket:', socket.id);
next();
});
// Add a route
const ns = wsServer.route(WebSocketController, /^\/chats\/\d+$/);
ns.use((socket, next) => {
console.log(
'Middleware for namespace %s - socket: %s',
socket.nsp.name,
socket.id,
);
export function createRecommendationServer(
port = 3001,
host: string | undefined = undefined,
) {
const app = express();
app.get('/:userId', (req: express.Request, res: express.Response) => {
let userId = (req.params as ParamsDictionary).userId || 'user001';
if (!(userId in recommendations)) {
userId = 'user001';
}
res.send(recommendations[userId] || []);
});
return new HttpServer(app, {port, host});
}