Error in Node.js script: "connect EINVAL": null
Please find below the code:
var http = require('http');
var https = require('https');
var url = require('url');
url = "http://maps.googleapis.com/maps/api/directions/json?origin=Central Park&destination=Empire State Building&sensor=false&mode=walking";
var querystring = require('querystring');
var apigee = require('apigee-access');
var chunk = '';
var resultJSON = '';
var result = ''; http.createServer(function(req, res) { res.writeHead(200, { 'Content-Type' : 'application/json' }); try { callLocationAPI(req, res); } catch (e) { console.log("Error:", e.message); res.end("Try again later", e.message); } }).listen(6868, '127.0.0.1'); console.log('Server running at http://127.0.0.1:6868/'); function getCoordinates(mainreq, mainres) { // https://apidev1.testcoke.com/tccc-googlemaps-geocode-version-1 // ?address={Street Address}, {City}, {State} var routePath = mainreq.url; //routePath = routePath.replace("/", ""); //routePath = routePath.replace(",", ""); var options = { path : routePath, method : 'GET' }; console.log('Query String is ' + routePath); var req = https.request(options); req.on('response', function(response) { response.setEncoding('utf8'); response.on('data', function(chunk) { }); response.on('end', function() { }); }); req.on('error', function(e) { console.log('problem with request: ' + e.message); }); req.end(); }
Kindly use the code tab to populate the code snippet. It eases readability.
Please confirm if below is the node script in your file.
var result = ''; http.createServer(function(req, res) { res.writeHead(200, { 'Content-Type': 'application/json' }); try { callLocationAPI(req, res); } catch (e) { console.log("Error:", e.message); res.end("Try again later", e.message); } }).listen(6868, '127.0.0.1'); console.log('Server running at http://127.0.0.1:6868/'); function getCoordinates(mainreq, mainres) { // https://apidev1.testcoke.com/tccc-googlemaps-geocode-version-1 // ?address={Street Address}, {City}, {State} var routePath = mainreq.url; //routePath = routePath.replace("/", ""); //routePath = routePath.replace(",", ""); var options = { path : routePath, method : 'GET' }; console.log('Query String is ' + routePath); var req = https.request(options); req.on('response', function(response) { response.setEncoding('utf8'); response.on('data', function(chunk) { }); response.on('end', function() { }); }); req.on('error', function(e) { console.log('problem with request: ' + e.message); }); req.end(); }
Also ensure if the function calls are accurate.
Since callLocationAPI is missing.
Hi @devesh.raghwan,
Use the express module, it has more features and capabilities.
http://maps.googleapis.com/maps/api/directions/json?origin=Central%20Park&destination=Empire%20State...
The following snippet is the working script to connect to the above end point. It makes a GET call on the end point specified.
var express = require('express'); var app = express(); var request = require('request'); app.get('/',function(req,res){ getCoordinates(req, res,function(err,response){ if(!err) res.send(response); else{ res.send({"message":"Error"}); } }); }) function getCoordinates(mainreq, mainres,callback) { var options = { method : 'GET', uri: 'http://maps.googleapis.com/maps/api/directions/json?origin=Central Park&destination=Empire State Building&sensor=false&mode=walking', }; request(options,function(err,res, body){ if (err || !res) return callback(); callback(null, body); }); console.log("Done"); } var server = app.listen(8081);
You may add further functionalities.
Keep us posted. Hope this helps, thank you.
Thank you for your answer.
User | Count |
---|---|
3 | |
2 | |
1 | |
1 | |
1 |