Im using NodeJS to perform som Queries to a MongoDB. I've set up the routing so when
http://127.0.0.1:8888/GetAllCities
is called, a function name GetAllCities will run on the server and perform a MongoDB query and return the result. This works just fine when I try to visit the page directly in my browser. But when I try to make a CURL request the response is empty.
This is my jquery Ajaxcall
function makeCall(urlToCall, method)
{
var params = "url=" + urlToCall + method;
if(app === "PHP")
{
params += "&PHP=PHP";
}
$.ajax({
type: "POST",
url: "phpProxy.php",
data: params,
beforeSend: function()
{
document.write(app + ' -- Starting round ' + (i+1) + "<br />");
document.write('URL: ' + params + "<br />");
startTime = new Date().getTime();
},
success: function(data) {
endTime = new Date().getTime();
totalTime = endTime - startTime;
if(totalTime > highest)
{
highest = totalTime;
}
if(totalTime < lowest)
{
lowest = totalTime;
}
var row = {};
row.app = app;
row.operation = method;
row.totalTime = totalTime;
row.run = i+1;
row.date = new Date();
document.write('\tTotal time: <strong>' + totalTime + 'ms</strong>' + "<br />");
times.push(row);
document.write(data + "<br />");
document.write('Round done\n' + "<br />" + "<br />");
},
error: function(err) {
console.log('Error');
console.log(err);
},
complete: function()
{
i++;
if(i < 50)
{
makeCall(urlToCall, method);
}
else
{
var sum = 0;
for(var j = 0; j < times.length; j++)
{
sum += parseInt(times[j].totalTime);
}
var avg = sum/times.length;
$('body').prepend('<h3>Slowest: ' + highest + 'ms</h3>');
$('body').prepend('<h3>Fastest: ' + lowest + 'ms</h3>');
$('body').prepend('<h3>Average: ' + avg + 'ms</h3>');
myApp.saveToDb(times);
}
}
});
}
And this is my PHPPROXY FILE:
$method = $_SERVER['REQUEST_METHOD'];
$url = $_POST['url'];
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL, $url);
curl_setopt($curl,CURLOPT_POST, TRUE);
if(isset($_POST['PHP']))
{
$params = array('a' => 'php');
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
And this is my NODEJS requesthandlerfile.
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var querystring = require("querystring");
function GetAllCities(response)
{
console.log('Request recieved');
var databaseUrl = "exjobb";
var collections = ["cities"]
var db = require("mongojs").connect(databaseUrl, collections);
db.cities.find(function(err, cities) {
response.writeHead(200, {"Content-Type": "application/json"});
response.write(cities.length + " rows selected from the Database");
response.end();
db.close();
});
}
function GetAllCitiesWhere(response)
{
var databaseUrl = "exjobb";
var collections = ["cities"]
var db = require("mongojs").connect(databaseUrl, collections);
db.cities.find({state: "AL"}, function(err, cities) {
response.writeHead(200, {"Content-Type": "application/json"});
response.write(cities.length + " rows selected from the Database");
response.end();
});
}
exports.start = start;
exports.GetAllCities = GetAllCities;
exports.GetAllCitiesWhere = GetAllCitiesWhere;
TLDR; My code works on a direct GET when visiting the page in a browser, but not when making ajax request, why? You can find all code here