Question

I have set up a local server in my machine using nodejs. something like this:

var fs = require("fs");
var config = JSON.parse(fs.readFileSync("config.json"));
var host = config.host;
var port = config.port;
var express = require("express");

var app = express();
app.get("/",function(request,response){
    response.send('thanks for visiting my  site');
});
app.listen(port, host);

Now, If I send a http request to this server, I get the 502 seervice unavailable response.

HttpRequest req = new HttpRequest();
 req.setEndpoint('http://localhost:4555/');
 req.setMethod('GET');
 Http http = new Http();
 HTTPResponse res = http.send(req);

I have also added the localhost url in the remote site setting, but still I am unable make any calls. Any idea how to go about it?

Was it helpful?

Solution

localhost is a relative address, which machine it resolves to depend on where it used, so when you use localhost in apex code (which is running on a salesforce server), its trying to talk to itself not your nodejs host. You need to give your host a publicly resolvable name or IP address and use that in your apex code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top