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').....
Related
I'm attempting to set up my first Node.js application on Windows Server. The application runs fine with the command:
node index.js
It'll run my application on localhost:8000. However, I want it to run on a subdomain on port 80, so I tried the following command:
set PORT=80 && set HOST=api.mydomain.com && node index.js
And here is my index.js file, which handles setting the PORT and HOST in the app.listen command:
import express from 'express';
import bodyParser from 'body-parser';
const expressJwt = require('express-jwt');
const config = require('./environment.json')[process.env.NODE_ENV || 'development'];
const app = express();
const port = +process.env.PORT || 8000;
const host = process.env.HOST || 'localhost';
// Skipping app.use commands...
app.get('/', root);
app.listen(port, host, 34, err => {
if (err) return console.error(err);
return console.log(`Server is listening on ${port}`);
});
Unfortunately, using a subdomain errors:
events.js:187
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND api.mydomain.com
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26) Emitted 'error' event on Server instance at:
at GetAddrInfoReqWrap.doListen [as callback] (net.js:1485:12)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:17) { errno: 'ENOTFOUND', code: 'ENOTFOUND', syscall: 'getaddrinfo',
hostname: 'api.mydomain.com ' }
What is the proper way to configure the Node.js application to run under the subdomain? If I were using IIS it'd be as simple as adding additional Bindings, but now I'm in Node.js land instead so have no idea how to go about it.
host
No for your application to run locally with this domain name you must modify the host configurations to make api.mydomaine.com correspond to the ip address 127.0.0.1 on your machine
I ended up just running it under IIS, using a reverse proxy. Then I could use IIS to set up the subdomain binding, much simpler...
https://dev.to/petereysermans/hosting-a-node-js-application-on-windows-with-iis-as-reverse-proxy-397b
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
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
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.
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.