A
A
Alexander Ivanov2022-03-18 15:03:38
Node.js
Alexander Ivanov, 2022-03-18 15:03:38

How to set up cors headers in node.js?

client

var socket=io('https://site.com:3000');

socket.on('connect', function (){
   code ... 
});


server
var fs = require('fs');
//var cors = require('cors');
var options = {
    key: fs.readFileSync('abe5c05b1.key'),
    cert: fs.readFileSync('b319649cbd8fb1a80.crt')
};
var http = require('https').createServer(options);
var io = require('socket.io')(http, {
  cors: {
    origin: 'https://site.com'
  }
});
var helper = require('./utils/helper');
http.listen(3000, function(){
    console.log('listening on *:3000');
});


.htaccess
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "https://site.com:3000"
Header add Access-Control-Allow-Headers *
Header add Access-Control-Allow-Methods "GET, PUT, POST, DELETE, HEAD, OPTIONS"
</IfModule>


when starting the server, for about half a minute the server thinks then throws an error socket.js:108
this.pingIntervalTimer.refresh();
TypeError: this.pingIntervalTimer.refresh is not a function

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bugs-bunny, 2022-03-18
@bugs-bunny

https://socket.io/docs/v3/handling-cors/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question