How to solve cors error in socket.io laravel and nodejs signalling app

Published

i have a chat app built with laravel and socket.io. My laravel app is located on one domain while my nodejs app is on another domain. Connecting to my nodejs signalling app gives a cors error while the nodejs app also returns cors error. Here is my nodejs app

"use strict";

require('dotenv').config();

const express = require('express');
const app = express();
const fs = require('fs');
const options = {
        key: fs.readFileSync(process.env.KEY_PATH),
        cert: fs.readFileSync(process.env.CERT_PATH)
    };
const https = require('https').Server(options, app);
const io = require('socket.io')(https);
io.origins('*:*');
const listner = https.listen(process.env.PORT, function() {
    console.log('Listening on ', listner.address().port);
});

//allow only the specified domain to connect
io.set('origins', process.env.DOMAIN + ':*');

require('./socket')(io);

app.get('/', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
   // Add this
   if (req.method === 'OPTIONS') {
        res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, OPTIONS');
        res.header('Access-Control-Max-Age', 120);
        return res.status(200).json({});
    }
    res.send('Ok');
  
    next();

I have installed a cors middleware on my laravel backend but no difference whatsover. Any help will be appreciated

Source: Laravel

Published
Categorised as cors, laravel, node.js, socket.io Tagged , , ,

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq