'Request must be a valid HTTP request' error while calling a node.js API proxy

We are creating a simple node.js api proxy where we want to fetch employee records.

We have added below code in the node :

var http = require('http');
var apigee = require('apigee-access');


var server = http.createServer(function(req, resp) {

var empID = apigee.getVariable(req,'emp_id');
//var empID="014AJ1F0027";

var employee_name = '';
var rating='';

console.log('Employee ID I have entered is %s', empID);

if (empID == "014AJ1F0027")
{ employee_name="Wesly";
rating="A";
}
if (empID == "015AJ1F0030")
{ employee_name="Karthikeyan";
rating="B";
}
if (empID == "016AJ1F0035")
{ employee_name="Kumar";
rating="A";
}
if (empID == "017AJ1F0049")
{ employee_name="Johnson";
rating="A";
}
apigee.setVariable("employee_name",employee_name);
apigee.setVariable("rating",rating);

resp.end();
});


port = 9000;
host = '127.0.0.1';
server.listen(port, host);

console.log('Listening at http://' + host + ':' + port);

Can you suggest, how I can do this in node.js?

0 2 314
2 REPLIES 2

Hi,

I'm going to put my answer in a couple of parts here:

1) I want to make sure I understand your question. Are you asking how to set variables? the setVariable function requires that you send in the request object as the first parameter which you've called `req` in your createServer call at the top. You did do this for getVariable further up.

2) If it were me I'd restructure this a bit. Create an object with all of the employees in it. Then just pull that entry out of the object instead of using a long if-chain.

3) Maybe you don't really want to use node for this? Since you're just reading and writing variables you could accomplish the same thing with a javascript-policy and then you don't need to worry about the rest of the server logic here...

I'm not sure I've answered your question or helped. If not lemme know and I'll endeavor to give a more thorough answer.