ambelovsky / koa-socket-2

Socket.IO for Koa
https://www.npmjs.com/package/koa-socket-2
98 stars 14 forks source link

No provision for CORS resolve #26

Open ankitinfostride opened 3 years ago

ankitinfostride commented 3 years ago

Please provide support for CORS errors !!

ambelovsky commented 3 years ago

Please provide server code samples.

ankitinfostride commented 3 years ago

Please find the code below:

import './env.js'
import Koa from 'koa'
import cors from '@koa/cors'
import hbs from '@nois/koa-hbs'
import serve from 'koa-staticfiles'
import bodyParser from 'koa-bodyparser'
import passport from 'koa-passport'
import logger from 'koa-logger'
import IO from 'koa-socket-2'

import router from './routes/index.js'
import cron from './lib/cron.js'
import { initDB } from './config/db.js'
import { port } from './config.js'
import { validateSuspended } from './config/middlewares.js'

initDB()
const app = new Koa()
const io = new IO()

const viewOptions = {
 viewPath: './views'
}

// Serving static files
app.use(serve('./views/assets/'))

// Attaching all middlewares
app
.use(hbs.middleware(viewOptions))
.use(cors())
.use(logger())
.use(bodyParser())
.use(router())
.use(passport.initialize())
.use(passport.session())
.use(validateSuspended)

// Attaching Socket
io.attach(app)

// Run scheduler
// cron()

io.on('connection', (ctx, data) => {
 console.log('Context: ', ctx)
 console.log('client sent data to message endpoint', data)
})

// Start the server
app.listen(port, () => console.log(`✅ The server is running at http://localhost:${port}/`))

export default app

Thank you for prompt response.

yangxyo commented 3 years ago

@ankitinfostride I did not find some right codes for me from yours. But I resolve it by this way:

  const IO = require("koa-socket-2")
  const io = new IO({
    ioOptions: {
      cors: {},
    },
  })
baaami commented 2 years ago

How do I put options in cors?

this code not working

const app = new Koa(); const io = new IO({ ioOptions: { cors: { origin: '*', }, }, }); io.attach(app);

thoasty-dev commented 2 years ago

Leave it empty and follow the instructions here https://github.com/ambelovsky/koa-socket-2#https-example to add cors headers

felipefsv commented 7 months ago

@ankitinfostride I did not find some right codes for me from yours. But I resolve it by this way:

  const IO = require("koa-socket-2")
  const io = new IO({
    ioOptions: {
      cors: {},
    },
  })

Hey! this worked for fe as well!