Quantcast
Channel: Latest Questions on Splunk Answers
Viewing all articles
Browse latest Browse all 13053

Client CORS proxy problem with express.js and Javascript SDK

$
0
0

The simple browser UI examples that work in the javascript SDK (using node sdkdo runserver) don't work in my express/node project, because I am not handling the proxy properly to get around the cross-origin resource sharing limitation. I have tried to do so, but haven't gotten it to work. In my app.js file I added

var request = require('request'); 
var splunkjs = require('splunk-sdk');

and then above my express app.get(...) routes, I added:

app.all('/proxy/*', function(req, res) {
    var error = {d: { __messages: [{ type: "ERROR", text: "Proxy Error", code: "PROXY"}] }};
    var writeError = function() {
        res.writeHead(500, {});
        res.write(JSON.stringify(error));
        res.end();
    };
    try {
        var body = "";
        req.on('data', function(data) {
            body += data.toString("utf-8");
        });
        req.on('end', function() {
            var destination = req.headers["X-ProxyDestination".toLowerCase()];
            var options = {
                url: destination,
                method: req.method,
                headers: {
                    "Content-Length": req.headers["content-length"],
                    "Content-Type": req.headers["content-type"],
                    "Authorization": req.headers["authorization"]
                },
                followAllRedirects: true,
                body: body,
                jar: false
            };
            try {
                request(options, function(err, response, data) {
                    try {
                        var statusCode = (response ? response.statusCode : 500) || 500;
                        var headers = (response ? response.headers : {}) || {};
                        res.writeHead(statusCode, headers);
                        res.write(data || JSON.stringify(err));
                        res.end();
                    }
                    catch (ex) {
                        writeError();
                    }
                });
            }
            catch (ex) {
                writeError();
            }
        });
    }
    catch (ex) {
        writeError();
    }
});

In the debugger I've verified that the app.all() function is entered, but the req.on('end') handler is never invoked. The browser sends

localhost:8888/proxy/services/auth/login?output_mode=json

but it hangs, pending, until it times out or I kill the web server.

How should I handle this?


Viewing all articles
Browse latest Browse all 13053

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>