1

I have a NodeJS app I am using as a game server.

I am trying to setup CORS with it, but app.use doesn't seem to be getting called.

Anyone know why?

var util = require("util");                 // Utility resources (logging, object inspection, etc)

var fs = require('fs');

var express = require("express");
var app = express();
var port = 3000;

app.use(function (req, res, next) {

    // these never get printed out:
    util.log( "app.use adding Access-Control-Allow-Origin" );
    console.log( "app.use adding Access-Control-Allow-Origin" );

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', 'https://example.com');

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);

    // Pass to next layer of middleware
    next();
    });

var server = app.listen(port, function(){
                    console.log('CORS-enabled web server listening on port ' + port);
                    });
var io = require('socket.io').listen(server);
Nitzan Wilnai
  • 923
  • 9
  • 24

1 Answers1

1

Checkout the npm cors package. https://www.npmjs.com/package/cors

Example usage where all requests will be CORS enabled:

var express = require('express')
  , cors = require('cors')
  , app = express();

app.use(cors());

app.get('/my_API_URL/:id', function(req, res, next){
  res.json({msg: 'This is CORS-enabled for all origins!'});
});

app.listen(80, function(){
  console.log('CORS-enabled web server listening on port 80');
}); 

On their page they also got other examples where the CORS are only enabled on a single route.

Also, just wondering how are you testing your application? You haven't defined any routes in the example code.


As pointed out in the comment section, @Nitzan Wilnai is not doing REST API, apologise for the confusion. It is suppose to be a simple server that listens on a certain port, so for this case you might not need express at all. Did some research and this solution came out;

io.configure('development', function(){
    io.set('origins', '*:*');
}

OR

io.set( 'origins', '*domain.com*:*' );

References: Socket.io doesn't set CORS header(s)

Just in case you are trying to build a chat program. Here is an example project; https://github.com/socketio/socket.io

Community
  • 1
  • 1
Samuel Toh
  • 18,006
  • 3
  • 24
  • 39
  • What do you mean by routes? Isn't it enough to call app.listen? – Nitzan Wilnai Jun 23 '16 at 03:47
  • 1
    Not really sure how you are invoking the server. From what I know is that you typically define an endpoint for your client to hit. E.g. app.get('money'/:amount). Then your REST client would do HTTP GET url=localhost/money/3000. This endpoint would be your route on the server. – Samuel Toh Jun 23 '16 at 04:39
  • 1
    Using io.on. For example: io.on("connection", OnSocketConnection); Then: function OnSocketConnection( pClient ) { m_pClient = pClient; util.log( "New player has connected: "+pClient.id ); } To send data to a particular player, we use: io.to( pPlayer.nodeID ).emit( pEventName, pData ); – Nitzan Wilnai Jun 23 '16 at 14:04
  • Ah, I re-read your comment and I think I am starting to understand. – Nitzan Wilnai Jun 23 '16 at 14:09
  • I am not using routes, I am using sockets. – Nitzan Wilnai Jun 23 '16 at 19:38
  • Ah I see. I saw you using express framework so I thought you're doing routing. I did some research for you and I think this solution should work for you. Checkout the updated solution. – Samuel Toh Jun 24 '16 at 02:35