1Click the source link, or copy and paste the link below to go to your Grepper
2Answers Page
3https://www.codegrepper.com/app/my_answers.php
1//working with node express many times we find cors issue even though we have installed cors package modules
2//this is the working example using with socket io
3
4var app = require('express')();
5var http = require('http').createServer(app);
6var io = require('socket.io')(http,{
7 cors:{
8 origin:"http://localhost:3000",
9 methods: ["GET","POST"],
10 allowedHeaders: [""],
11 credentials: true
12 }
13});
14const cors = require('cors');
15
16// creating web socket
17io.on('connection',(socket)=>{
18 console.log('User Online');
19
20 socket.on('canvas-data',(data)=>{
21 socket.broadcast.emit('canvas-data',data);
22 })
23})
24
25// const app= express();
26app.use(cors());
27
28
29var server_port = process.env.YOUR_PORT || process.env.PORT || 5000;
30http.listen(server_port,() =>{
31 console.log("Started on : "+server_port);
32})
33// const PORT = 3001;
34// app.use(cors());
35
36
37// app.get("/", (req, res)=>{
38// res.send("hell of code");
39// });
40
41// app.listen(PORT, ()=>{
42// console.log(`Server running on port ${server_port}`);
43// })
44