GeoJson from Geoserver using node.js - node.js

I am new to Node.js, learning with examples.
Here is what I am trying to do, I have a geoserver running to serve GeoJson, I want to call geoserver WFS url and get json data using node.js. Here is code, when I run it, I get :
getaddrinfo ENOENT
var http = require('http');
var options = {
host: "local:8080/geoserver/wfs?service=WFS&version=1.0.0&request=GetFeature&typeName=layername&outputFormat=JSON&cql_filter=id=1";
path: '/'
}
var request = http.request(options, function (res) {
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
console.log(data);
});
});
request.on('error', function (e) {
console.log(e.message);
});
request.end();
Please guide me in right direction. Thank you.

You need to pass in the correct options:
host - should only be the host name
path - should the path to the resource on the host (all the stuff you have after the host name
method - should be GET or POST (GET in your case).
var options = {
host: "local:8080";
path: '/geoserver/wfs?service=WFS&version=1.0.0&request=GetFeature&typeName=layername&outputFormat=JSON&cql_filter=id=1',
method: 'GET'
}

I was also trying to get a GeoJSON via Node.js and took a different approach; I used Express and sequelizejs. This allowed me to get objects directly from Postgres / PostGIS. I needed to do a little formatting client-side to form a valid GeoJSON from the express response.

Related

Get the result outside of the http call

In our project, we make a call to a REST web service with Node using HTTP request. We can see the result when we log it into the console. But we would like to store it or use it outside of the HTTP request. How can we achieve that?
var http = require('http');
var options = {
host: 'http:\\example.com',
port: 80,
path : '/site/status?tag=A7351&date=09JAN&name=LASTNAME',
method : 'GET'
};
var result;
http.request(options, function(res){
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
result = JSON.parse(body);
console.log(result, "INSIDE"); //Shows the JSON result
});
}).end();
console.log(result, "OUTSIDE"); //Shows Undefined
The result is in this format:
{
error: 0,
statut: 'STATUTTTTT',
origine: 'PLACEEE',
destination: 'PARIS',
onwardDate: null
}
We need to be able to get the result outside of the HTTP call.
The issue is that you're making an asynchronous call, but not handling it correctly. Dialogflow's library requires that when you make an async call you return a Promise.
The easiest solution is for you to switch from using the request library to using the request-promise-native library and to return the Promise.
See other answers on Stack Overflow for examples and more information.

Getting Checking your browser before accessing template and 503 status code with https.request(options,callback) node.js

I want to get the html of this page for parsing(click the link to understand what content i want to get).
750-bond list
Here's my code to request this page content
var https = require("https");
var fs = require("fs");
var options = {
hostname: "www.prizebond.net",
port: 443,
path: "/dlist.php?num=455",
method: "GET"
};
var response = "";
var req = https.request(options, function (res) {
res.setEncoding("UTF-8");
console.log(res.statusCode);
res.on("data", function (chunk) {
response += chunk;
});
res.on("end", function () {
fs.writeFile("750-bond.html", response, function (err) {
if (err) {
console.log(err.message);
}
console.log("File downloaded");
});
console.log("end");
});
});
req.end();
Now the problem is that in my 750-bont.html file, I am getting the weird the
result of "Checking your browser before accessing the prizebond.net" not the
original content. Here's the screenshot what I got when I open the 750-
bond.html file in browser.
What I am doing wrong? And how can I get the original content of this webpage?
You can't, unless you write something more sophisticated, but you probably shouldn't.
The purpose of Cloudflare-protection is to prevent what you are trying to realize unfortunately.
You could look into a possibility to access whatever you want to access by a public API or something that prizebond.net provides for example.

invalid-input-secret for google's recaptcha api

I'm trying to use recaptcha on my website. Nodejs server with express framework. The site isn't being hosted, I'm still working on it locally. On the homepage, after the user enters his info to create an account, and solves the recaptcha, I send the results
$("#g-recaptcha-response").val()
to the server. And on my server,
https.get("https://www.google.com/recaptcha/api/siteverify?secret=" + SECRET + "&response=" + key, function(res) {
var data = "";
res.on('data', function (chunk) {
data += chunk.toString();
});
res.on('end', function() {
try {
var parsedData = JSON.parse(data);
console.log(parsedData);
callback(parsedData.success);
} catch (e) {
callback(false);
}
});
});
where key is the response and SECRET is the secret key they give you. I declared
a variable SECRET and stored the secret key as a string in it.
Every single time, the for the
console.log(parsedData);
It's saying
{ success: false, 'error-codes': [ 'invalid-input-secret' ] }
I copied and pasted the secret key, how could it be invalid. It's only supposed to show this error if "The secret parameter is invalid or malformed" as it says on their website. I followed this tutorial.
I followed the tutorial too and then bumped into the same error that you have reported here. Looking closely at the screenshot in the tutorial it shows
Send a GET request with these parameters
And checking the Google reCaptcha website it says
Send a POST request with these parameters
I am curious whether Google changed their mind about POST instead of GET or the screenshot in the tutorial is from a different source.
Regardless, I have tweaked the version of code in the tutorial to make POST request (below code uses querystring module), see below:
var SECRET = "YourSecretHere";
// Helper function to make API call to recatpcha and check response
function verifyRecaptcha(key, callback) {
//declare above var querystring = require('querystring') on top
var post_data = querystring.stringify({
'secret' : SECRET,
'response': key
});
var post_options = {
host: 'www.google.com',
port: '443',
method: 'POST',
path: '/recaptcha/api/siteverify',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data)
}
};
var req = https.request(post_options, function(res) {
var data = "";
res.on('data', function (chunk) {
data += chunk.toString();
});
res.on('end', function() {
try {
var parsedData = JSON.parse(data);
callback(parsedData.success);
} catch (e) {
callback(false);
}
});
});
req.write(post_data);
req.end();
req.on('error',function(err) {
console.error(err);
});
}
I also wanted to add that remoteip field is optional but, you can pass that value too if you want too. In order to do that, you need to retrieve the remoteIpAddress from connection object or simply enable trust proxy on your app as shown below:
app.enable('trust proxy');
and then pass the ip address to the verifyRecaptcha and the call would look like follow:
verifyRecaptcha(req.ip, req.body["g-recaptcha-response"], function(success) {
if(success) { ...} else { ... }
});
You then need to modify the post_params to include remoteip field as follow:
var post_data = querystring.stringify({
'secret' : SECRET,
'response': key,
'remoteip': ip
});
app.enable('trust proxy'); allows req.ip and req.ips which is an array of ip addresses. For more info on getting the ip address of request see this SO question.
If you are developing and you get fed up with all tricky famous and the most annoying Street Names reCaptcha, then I recommend that you use the test Site and Secret keys provided by Google to override captcha solving in order to speed up development. See here
This is really stupid, and I can't believe I wasted this much time on it but instead of using the variable SECRET, I just added my secret key to the url and it worked.

Best practice for using API

I have some CRUD controllers set up in my project like this:
var getMaps = function (req, res) {
Map.find({}).exec(function (err, collections) {
res.send(collections);
});
};
In order to use these in the server side code, I've been using node's http.get like this:
var options = {
host: 'localhost',
port: 3030,
path: '/api/map'
};
http.get(options, function (res) {
var data = '';
res.on("data", function (chunk) {
data += chunk;
}).on('end', function () {
data = JSON.parse(data);
console.log(data);
});
}).on('error', function (e) {
console.log("Got error: " + e.message);
});
where my routes file contains
app.get('/api/map', map.getMaps);
Is this the correct way of doing it? It seems like it would be slow since it is an http request but I don't know any other way to do it
Better you go through $http and $resource links once to have a better understanding of angular's APIs.
You can also try out the MEANs CRUD module which comes by default when you generate a MEAN application.

node, socket.io - connect to list of news feed urls?

I wish to connect to the list of news feed urls via node and get the real-time data via socket.io. For that I tried with single url in server.js as below:
var http = require("http");
var options = {
host: 'http://economictimes.feedsportal.com/c/33041/f/534037/'
};
http.get(options, function (http_res) {
// initialize the container for our data
var data = "";
// this event fires many times, each time collecting another piece of the response
http_res.on("data", function (chunk) {
// append this chunk to our growing `data` var
data += chunk;
});
// this event fires *one* time, after all the `data` events/chunks have been gathered
http_res.on("end", function () {
// you can use res.send instead of console.log to output via express
console.log(data);
});
});
When I execute node server.js, it throws me an error
"Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)"
Is there any way to pass each news feed url from an array to connect it via node and get latest news via socket.io ???
From the node doc for the http module, this is what a typical options object looks like:
var options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
};
Per the doc, the host option you are using should be A domain name or IP address of the server to issue the request to. Defaults to 'localhost'. So, it looks like you just aren't calling .get() correctly.
If you just want to pass the whole URL, then don't use the options object, just pass the URL like this and the method will parse the URL for you into the relevant parts:
http.get('http://economictimes.feedsportal.com/c/33041/f/534037/', function (http_res) {...});

Resources