Open ankitinfostride opened 3 years ago
Please provide server code samples.
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.
@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: {},
},
})
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);
Leave it empty and follow the instructions here https://github.com/ambelovsky/koa-socket-2#https-example to add cors headers
@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!
Please provide support for CORS errors !!