Estou tentando fazer uma aplicação realtime com express (^4.17.1"), node (10.12.0) usando typescript.
Só que só no socket que está dando problema de cors
Access to XMLHttpRequest at 'http://localhost:3000/socket.io/?EIO=3&transport=polling&t=Mo_nwPh' from origin 'http://localhost:4200' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
Minha classe de configuração é essa aqui
class Aplicaccao {
app: express.Application
port = 3000
private io: SocketIO.Server
private server: Server;
constructor(){
this.app = express()
this.server = createServer(this.app);
this.io = socketIo(this.server);
}
listen(){
this.middleware()
this.realtime()
this.routes()
this.app.listen(this.port, ()=>{
console.log(`System is listening on port ${this.port}`);
})
}
middleware(){
this.enableCors()
this.app.use( bodyParser.json() )
this.app.use( bodyParser.urlencoded( {extended: true} ) )
}
enableCors(){
const options: cors.CorsOptions = {
"origin": "*",
"methods": "*",
"preflightContinue": false,
"optionsSuccessStatus": 200
}
this.app.use(cors(options))
}
realtime(){
this.io.on('connect', socket =>{
console.log('socket',socket);
socket.on('read', msg =>{
socket.broadcast.emit('read', msg)
})
})
}
routes(){
this.app.get('/', (req, res)=>{
res.send({obs: 'Bem vindo a api do Sistema de Notificações'})
})
this.app.use('/api', ...routes)
}
}
export const server = new Aplicaccao()
A chamada no angular está assim:
this.socket = io( 'http:localhost:3000' )
E no javascript assim:
var socket = io('http://localhost:3000')
Como pode ser problema CORS se os outros módulos da API funcionam: Consulta, insert, update?
Onde posso estar errando?