How to make a GET URL request (same server) - node.js

I'm running an express 4 app (nodejs)
In one of the server side functions, I want to make a GET request to get some value. The GET request url is pointing at the same server. Why this doesn't work and how do I make it work?
Extra info:
http://mywebsite/ is the main domain of my web app.
http://mywebsite/abc/getData/16-10-2017 works in the browser
Externally facing requests work fine (so no issue with request setup)
function testing(){
dateVAL="16-10-2017"
var requestURL = "http://mywebsite/abc/getData/" + dateVAL
request({
url: requestURL,
method:"GET",
},
function(error,response,body){
console.log("error")
console.log(error)
console.log("response")
console.log(response)
console.log("body")
console.log(body)
});
}
Error code:
{ Error: getaddrinfo ENOTFOUND mywebsite mywebsite:80
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'mywebsite',
host: 'mywebsite',
port: 80 }

Found a solution, modified my address from:
var requestURL = "http://mywebsite/abc/getData/" + dateVAL
to:
var requestURL = "http://localhost/abc/getData/" + dateVAL

Related

How can I read the raw HTTP response in a subpath with node?

I am trying to get a malformed HTTP response using node. Since the response is malformed, I cannot use request normally (it would give the HPE_INVALID_CONSTANT error)
From this answer https://stackoverflow.com/a/23543522/1748450 I can get the raw HTTP response using the net module like so:
var net = require('net');
var host = '192.168.1.1',
port = 80,
socket = net.connect(port, host, function() {
var request = "GET / HTTP/1.1\r\nHost: " + host + "\r\n\r\n",
rawResponse = "";
// send http request:
socket.end(request);
// assume utf-8 encoding:
socket.setEncoding('utf-8');
// collect raw http message:
socket.on('data', function(chunk) {
rawResponse += chunk;
});
socket.on('end', function(){
console.log(rawResponse);
});
});
However, this only works with getting the response from the host's root page (192.168.1.1). The page I'm trying to get the response from is actually 192.168.1.1/admin/landingpage.fwd.
If I try to edit host to that URL then I get this error:
events.js:187
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND 192.168.1.1/admin/landingpage.fwd
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26)
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: '192.168.1.1/admin/landingpage.fwd'
}
Is this possible to fetch this using the net module in the above example?
If not possible, what other way can I use to get the raw HTTP response from that URL?
You can just put the path in your http request as in:
var request = "GET /admin/landingpage.fwd HTTP/1.1\r\nHost: " + host + "\r\n\r\n",?
That's where the path goes in an http request.

Nodejs Https get Error: getaddrinfo ENOTFOUND

I'm trying to get a filtered record from loopback, but I don't understand why nodejs gives error on fallowing commands:
const https = require('https');
var uid = '02644da038b37d7ba70b7ee1a92ba1d9';
var URL = 'https://mobileapp.mydomain.com/api/uuids?filter[where][uuid]='+uid;
https.get(URL, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
}).on('error', (e) => {
console.error('ERROR:',e);
});
the error on output:
ERROR: { Error: getaddrinfo ENOTFOUND mobileapp.mydomain.com mobileapp.mydomain.com:443
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:79:26)
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'mobileapp.mydomain.com',
host: 'mobileapp.mydomain.com',
port: 443 }
Obviously the domain you used is not valid.
URL shouldn't contain https://
modify url from
var URL = 'https://mobileapp.mydomain.com/api/uuids?filter[where][uuid]='+uid; to var URL = 'mobileapp.mydomain.com/api/uuids?filter[where][uuid]='+uid;
I had this issue and this resolved it.
probably it's because of SSL authentication in loopback, can you please try this npm package

ECONNRREFUSED Error when making a get request to an API with Nodejs

I did make a get request to an API but get an ECONNREFUSED Error. The problem is not the API because when I type it in a browser, I get back results in JSON.
This is my code;
var https = require("https");
var options = {
host : 'nairabox.com',
port : 443,
path : '/v1/tickets/auth=APIKEY&as=showtimes&cinemaId=CINEMAID&ticketId=TICKETID',
method : 'GET'
}
var req = https.request(options, function(res) {
res.on('data', function(data) {
console.log( JSON.parse(data));
});
});
req.end();
req.on('error', function(err){
console.log("Error: ", err);
});
This is the error;
Error: { Error: connect ECONNREFUSED 162.255.119.75:443
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '162.255.119.75',
port: 443 }
Anyone can be kind enough to test it replacing the parameters with random numbers and you'd get the same error. How can I fix it. API gives results in the browser though.
The hostname nairabox.com resolves to two IP-numbers, 178.79.175.127 and 162.255.119.75. The latter is refusing connections, which is the issue you're running in to.
However, the host www.nairabox.com resolves to only one IP-number, 178.79.175.127, so I guess you should be using that hostname instead of the one without the www. prefix.

When I try to do a https post request in node, I get a getaddrinfo ENOTFOUND error

The full error is this:
events.js:160
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND https://api.instagram.com https://api.instagram.com:443
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:79:26)
and my code to get just the error is this:
var express = require('express');
var app = express();
var https = require('https');
app.get('/oauth/ig', function (req, res) {
var options = {
hostname: 'https://api.instagram.com',
path: '/oauth/access_token?client_secret=myClientSecret&grant_type=authorization_code&redirect_uri=http://localhost:1992/oauth/ig&code='+req.query.code,
method: 'POST',
};
var igRequest = https.request(options, (response) => {
res.send("response");
});
})
I am using nodejs and am trying to do the Instagram OAUTH.
hostname should be just the host name, not including the protocol:
hostname: 'api.instagram.com',
In addition to the answer by mscdex, it's worth mentioning that
there are already working modules on npm that do that, e.g.:
passport-instagram
passport-instagram-token
instagram-node
Maybe you could use one of them instead of rolling your own solution.
There is nothing wrong about rolling your own solution but if you can't get it right, sometimes it's just not worth the hassle.
Two of the modules that I recommended above are Instagram strategies for Passport, which is a de facto standard way of doing authentication in Node, with over 300 different strategies available that you can use in a unified way.
See the Passport website for more info.

superagent relative path server-side

I am trying to get superagent to work on the server side with relative path but it's not playing nicely.
The thought is, I need to proxy from the frontend to the backend with routes /api/* being the proxy route. When doing a superagent request such as:
request.get('/api/surahs')
.end(function(err, res) {
debug('SURAHS RECEIVED....');
console.log(err);
actionContext.dispatch('surahsReceived', {surahs: res.body, surah: payload});
});
I always get error
[1] { [Error: connect ECONNREFUSED]
[1] code: 'ECONNREFUSED',
[1] errno: 'ECONNREFUSED',
[1] syscall: 'connect',
[1] response: undefined }
Any ideas?
i ran into same issue. You can get a host from express req like req.get('host').
Than you can create SSR request like:
request.get(req.get('host') + '/api/surahs').....

Resources