dimanche 26 avril 2015

Connecting to node.js server on same AWS instance different port, connection refused

I am trying to connect to a node.js server on the same machine as my web server, on an AWS Amazon linux instance. When I try to use Ajax to communicate to the server at 127.0.0.1:8003 or localhost:8003, I get a "Cross-Origin Request Blocked" error with Firefox, and "net::ERR_CONNECTION_REFUSED" with Chrome.

I get the expected response if I access the machine from a browser directly (i.e. http:[server]:8003). Also I get the expected response with curl localhost:8003 from the machine. I only see the problem with the ajax connection.

Right now the node.js server is trivial:

var sys = require('sys');
var http = require('http');

var server = http.createServer(
    function( request, response ) {

        var origin = "*"; // (request.headers.origin || "*");

        if (request.method.toUpperCase() === "OPTIONS"){
            // Echo back the Origin (calling domain) so that the
            // client is granted access to make subsequent requests
            // to the API.
            response.writeHead(
                "204",
                "No Content",
                {
                    "access-control-allow-origin": origin,
                    "access-control-allow-methods": "GET, POST, OPTIONS",
                    "access-control-allow-headers": "content-type, accept",
                    "access-control-max-age": 10, // Seconds.
                    "content-length": 0
                }
            );

            // End the response - we're not sending back any content.
            return( response.end() );

        }
        responseBody = "Hello world! from X AWS - version 2\n";
        response.writeHead(
            "200",
            "OK",
            {
                "access-control-allow-origin": origin,
                "content-type": "text/plain",
                "content-length": responseBody.length
            }
        );

        response.write( responseBody );
        response.end();
    }
);

server.listen(8003);
console.log("Server running on 8003...");

There is some extra code in there trying to see if the problem was with the access-control-allow-origin header.

The client page is simple too:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>X BC API Tester</title>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>


    <script type="text/javascript">
    function call_bc_proxy()
    {
        console.log("Calling bc_proxy...");
        $.ajax({
            type: "GET",
            url: "http://localhost:8003",
            dataType: "text",
            success: function( response ){
                $( "#cms_response" ).html( response );
            },
            error: function( error ){

                // Log any error.
                console.log( "ERROR:", error );

            },
        });

        return false;
    }
    </script>

</head>
<body>
    <p>Test new BCOV API using oAuth2</p>
    <p><a href="#" onclick="return call_bc_proxy();" class="button">Get token</a></p>
    <div id="cms_response"></div>
</body>
</html>

I think I can workaround the problem by calling a PHP function via Ajax, and using curl within the PHP function, but I would like to get it to work directly with Javascript.

Any ideas appreciated. Not sure what other info would be useful.

jd




Aucun commentaire:

Enregistrer un commentaire