Communicate with API behind a proxy in Ionic - node.js

I am using ionic config as follows to connect with API. But the problem is I am behind a company proxy. As a result of it I can't connect to API.
{
"proxies": [
{
"path": "/v1",
"proxyUrl": "https://api.instagram.com/v1"
}
]
}
For an example if the company proxy is 192.168.1.18:8080, how can get it working?
After reading the Ionic node module code I figured it out, Ionic is using proxyMiddleware to do the proxy. However I can't find a way to set proxy to proxyMiddleware node module.

You can try use this library: https://github.com/nodejitsu/node-http-proxy

Related

How do I use a SOCKS proxy with npm got package?

I'm using npm got package to do web scraping and I want to use a SOCKS proxy. The docs have this snippet about using proxies:
import got from 'got';
import {HttpsProxyAgent} from 'hpagent';
await got('https://sindresorhus.com', {
agent: {
https: new HttpsProxyAgent([skipped by me])
}
});
So I've tried using socks-proxy-agent, but apparently the agent property of got only supports http, https and http2 keys.
How do I make got use my SOCKS agent?

How to configure node-fetch to use the company proxy?

I am executing a standalone nodejs script (no web server involved) that needs to fetch result from a third party api. The program uses 'node-fetch' to do the fetch(url) and I run it using node .\test.js from command line.
It fails when I am connected to our company network but works fine on direct internet. I have configured the proxy settings in npm and could see that npm config ls shows the correct values for proxy and https-proxy.
So the questions are:
1. Does running the test.js via node not pick the proxy config from npm?
2. How to make sure that the fetch(url) call goes through our proxy?
Thanks in advance
This worked for me, try using this :
https://github.com/TooTallNate/node-http-proxy-agent
The request formed will be similar to this:
fetch('accessUrl', {agent: new HttpsProxyAgent('proxyHost:proxyPort')})
.then(function (res) {
})

API in nodejs Port Issue

I've created an API in nodejs backend and frontend I used reactjs as I've connected the api to my form
it shows the error
"Could Not proxy request"
Check what is the error in Chrome developer tools. It is most probably CORS issue.
If you are using NodeJs, you can use below command.
res.header("Access-Control-Allow-Origin", "*");
But this will allow CORS for all port running on your machine.
I faced the same issue when using express. I resolved it as shown below :
TRY THIS OUT IN YOUR PACKAGE.JSON CONFIGURATION refereed from create-react-app
"proxy": {
"/api": {
"target": "https://localhost:5002",
"secure": false
}
},
Also try add CORS configuration to your node check this video

How to setup a proxy using web sockets and angular CLI

I have a simple web app built using the angular CLI. I want it to communicate with a backend using web sockets. I have the backend already written and have tested with a simple index.html page that the server can send and receive on sockets.
In my angular-cli project I have setup a proxy config file to setup a proxy to the backend.
proxy.conf.json
{
"/sock": {
"target": "http://localhost:3000",
"changeOrigin": true,
"ws": true,
"logLevel": "debug"
}
}
Then start the server with the following.
ng serve --proxy-config proxy.conf.json
For now I have a service that simply attempts to open a socket and send a fixed string which I'm expecting to see logged by the backend.
import { Injectable } from '#angular/core';
import * as io from 'socket.io-client';
#Injectable()
export class ChatService {
private socket: any;
constructor() {
this.socket = io({ 'path': '/sock' });
this.socket.emit('chat message', 'Hello World from browser!');
}
}
Note: I've had several go's at this with and without the /sock part of the url.
I start both servers. Get no console errors in the browser. But in the angular CLI web pack server I get the following messages.
10% building modules 2/2 modules 0 active[HPM] Proxy created: /sock -> http://localhost:3000
[HPM] Subscribed to http-proxy events: [ 'error', 'close' ]
[HPM] GET /sockjs-node/530/z1z3teld/websocket -> http://localhost:3000
[HPM] Upgrading to WebSocket
[HPM] Error occurred while trying to proxy request /sockjs-node/530/z1z3teld/websocket from localhost:4200 to http://localhost:3000 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
Are web sockets supported or have I made a silly mistake?
Thanks
I managed to figure it out with a bit of trial and error. I looked at the console for the basic index.html page that works within the backend project. This backend project is basically the chat server demo application on the socket.io website. I noticed that when it opens up the web socket the url looks like the following:
http://localhost:3000/socket.io/EIO=3&transport=websocket&sid=wTvdQTclHXJSUmAmAAAA
So back in the angular CLI project I modified my proxy config to include the /socket.io/ part plus also added a wildcard.
{
"/sock/*": {
"target": "http://localhost:3000/socket.io/",
"ws": true,
"logLevel": "debug"
}
}
Bingo! Now when the service is constructed it opens the socket and emits a message which I can see logged in the backend.

Not able to proxy REST API request to backend server with latest angular-cli server

I am new to Angular 2 and just created project using latest angular-cli (angular-cli: 1.0.0-beta.16
node: 6.7.0) on Windows machine. I was trying to call my back-end server REST API in one of angular service, but it could not route my request from localhost:4200 to localhost:8080/dz/api/...
I read about proxy To backend support in angular-cli document and followed the same steps. I do have now proxy.conf.json file at package.json file level.
{
"/rest": {
"target" : "(http)://localhost:8080/dz/api/v1",
"secure" : false
}
}
and I have updated package.json file with
"start": "ng serve --proxy-config proxy.conf.json",
and then on command prompt - npm start
But, when I call the service, it does not send request to back-end server and instead throws error with 404 (not found error - (http)://localhost:4200/rest/home/list).
I would appreciate if someone can help me out here what am I doing wrong here. Am I missing any other setting? I am new to Angular 2.
I did use http-proxy-middleware when I was working with angular1.3 and it was working fine.
Thanks.

Resources