FetchError: graphql failed, reason: unable to verify the first certificate - node.js

i'm using the graphql-request npm package in order to use graphql in my software.
i have the following line in my code:
const client: GraphQLClient = new GraphQLClient(process.env.OCEAN_ENDPOINT, {});
client.setHeaders({ Authorization: `Bearer: ${token}` });
and then i want to fire a request i use:
await client.request(query, variables);
until two days ago my endpoint was an http url, but now it changed to https and from that moment i'm getting this error:
FetchError: request to https://{graphqlEndpoint}/v1/graphql failed, reason: unable to verify the first certificate
has anyone faced this issue before?

NODE_TLS_REJECT_UNAUTHORIZED = "0";
adding this in env worked for me in nextjs project

Related

NodeJS HTTP Request force TLS version 1.1

I'm having problem with sending request using TLSv1.1. I've tried many solutions on the internet and no luck. I have to use only TLSv1.1 on specific requests because some of my services are legacy services.
Error: write EPROTO 98230000:error:0A000102:SSL routines:ssl_choose_client_version:unsupported protocol
This is the error I've been facing.
1. I've used axios and set httpsAgent like this.
const axios = require('axios');
const httpsAgent = new https.Agent({
maxVersion: 'TLSv1.1',
minVersion: 'TLSv1.1',
});
const response = await axios.post('URL', payload, { httpsAgent });
2. Bunch of other dependencies and NodeJS https module.
I've tried few dependencies that can send request to API and still getting same error.
3. --tls-min-v1.0' option
Tried NODE_OPTIONS='--tls-min-v1.0' yarn dev still no luck.
OS: Windows 10
NodeJS Version: v18.12.1

Connecting front end and back end React / Node.js

I am trying to create an account through my registration form, however I keep on getting this error:
This is my registration form:
However when I press Register, get this error:
Commands: npm start
Errors: Proxy error: Could not proxy request /users/register from localhost:3000 to http://localhost:5000.
See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).
Error photo:
Much appreciated, thank you in advance.
Problem solved: Needed to start the React and the Node project on two different terminals, first run the React and then the Node project
Enable cors in your server
if Node and express
then add cors package and then in the entry point file ( index.js or app.js )
add this along with other imports
const cors = require('cors');
and then after your app is initialised then add this line
app.use(cors());
and also add these lines to the header of your request on the react side
'Access-Control-Allow-Origin': '*',
if you are using axios then I suggest you creating a separate file for axios
import axios from 'axios';
export default axios.create({
baseURL: 'http://localhost:8000',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
}
});
there you configure your common axios details and import this file instead of the package wherever you using axios on react
If still facing an issue then please copy paste the error as it shows on the network tab
I think the solution to this is to run node server.js instead of npm start

Querying information about specific version of scoped npm package

I can successfully query meta information for a specific version of a specific NPM package like this:
GET https://registry.npmjs.org/<name>/<version>
for example: https://registry.npmjs.org/camelcase/2.1.1
But for scoped packages like #angular/core this doesn't work. I tried all of the following, but they all fail:
https://registry.npmjs.org/#angular/core/6.1.10 - 401 Unauthorized
https://registry.npmjs.org/#angular%2Fcore/6.1.10 - 401 Unauthorized
https://registry.npmjs.org/%40angular%2Fcore/6.1.10 - 401 Unauthorized
https://registry.npmjs.org/%40angular%2Fcore%2F6.1.10 - 404 Not Found
What is the correct way for querying a specific version of a scoped package?
You can do this from a bash command:
npm view #angular/core/6.1.10
So npm is adding some authentication to the request for scoped packages. For this to work you have to have a valid package.json in the local directory.
Of course, worst case, you can do a process.spawn() to run the npm command.
FYI, I tried using the npm-registry-client package, with my npm credentials:
var RegClient = require('npm-registry-client')
var client = new RegClient({
username: 'mememe',
password: 'xxxxx'
})
var uri = "https://registry.npmjs.org/#angular/core/6.1.10"
var params = {timeout: 1000}
client.get(uri, params, function (error, data, raw, res) {
console.log(data);
})
and I got this:
info attempt registry request try #1 at 09:52:09
http request GET https://registry.npmjs.org/#angular/core/6.1.10
http 401 https://registry.npmjs.org/#angular/core/6.1.10
WARN notice ERROR: you cannot fetch versions for scoped packages
It appears they don't allow specific version querying, but per #RobC's comments below, they do allow grabbing the entire repository's information, so you can do this client-side:
url = 'https://registry.npmjs.org/#angular%2fcore';
const fetch = require('node-fetch');
fetch(url).then(response => response.json()).then(results => {
console.log(results.versions['6.1.10']);
});

How to debug request-promise which fail with ETIMEDOUT

I'm running Debian 6 with NodeJS6 and request-promise.
I'm using request-promise to deliver web hooks. Currently, we are delivering around 2+M web hooks each day.
Most of the time everything runs fine, but there are cases where the HTTP request simply isn't delivered.
This is my simple code:
requestPromise({ url: 'https://change-iot-request/do', auth: { user: "switch", pass: "my-password" }, method: "GET", timeout: 15000, rejectUnauthorized: false})
.catch(function(err) {
console.log(err);
});
Sometimes the endpoint simply doesn't receive anything.
I've tried to run tshark to see if packages came through - nothing on any of the servers.
If I when it occurs run curl command with the same parameters the request DOES come through.
And if I restart the NodeJS it starts to deliver correctly again.
This is the error output:
{"name":"RequestError","message":"Error: ETIMEDOUT","cause":{"code":"ETIMEDOUT","connect":true},"error":{"code":"ETIMEDOUT","connect":true}
What to do?
Especially when curl does work so the connection should be fine.
it can be a lot of causes, maybe it's not node fold at all
ETIMEDOUT means that your server do not get response for request
you can try set a timeout, something like
var rp = require('request-promise');
var reqPromise = rp({
method: 'get',
uri: 'http://localhost:8080/test-connection-length',
timeout: 600000, // 10 min.
resolveWithFullResponse: true
});
or install node-retry and your server will re-run automatically when it's needed

Why would I be getting `Error: write EPROTO` when making an HTTPS request in an Electron app?

I've been pounding my head against the wall for days on this so I am turning to the smart folks at Stackoverflow to help. Here's the deal:
System Details
Node Version (can't be changed due to Electron dependencies): v4.1.1
Electron Version: v0.34.3
OS Version: Mac OSX Yosemite 10.10.5 (14F1021)
Issue Description
I'm building an Electron app that has to communicate with my company's application server. The server connection has to go over HTTPS. I'm using Node's built-in https module. When making a request to the server I'm getting the following error:
{ [Error: write EPROTO]
code: 'EPROTO',
errno: 'EPROTO',
syscall: 'write',
address: undefined }
I've done a ton of Googling on this and most everything I've found points to proxies but I'm not using a proxy. I've tried the following:
Setting rejectUnauthorized: false in the options hash
Modifying the secureProtocol option (no results)
Attempting to set the --tls-cipher-list (no idea what I'm doing there)
I can make the request over curl without issue. Unfortunately, I can't post the actual URL I'm making requests to.
Sample Code
Here's some sample code (Coffeescript) that illustrates the issue:
https = require 'https'
options = {
host: '[Application URL]'
path: '/'
method: 'GET'
port: 443
}
options.agent = new https.Agent(options)
callback = (response) ->
str = ''
console.log response
console.log "STATUS: #{response.statusCode}"
console.log "HEADERS: #{JSON.stringify(response.headers)}"
response.setEncoding 'utf-8'
response.on 'data', (chunk) -> str += chunk
response.on 'end', -> console.log str
makeRequest = ->
req = https.request options, callback
req.on 'error', (err) ->
console.log err
req.end()
makeRequest()
Does anyone have any idea what could be causing this issue? Is it a Node issue or something with the configuration of the application server? This bug is killing me and preventing me from hitting a milestone at work so any help would be greatly appreciated. Thanks!
To resolve this issue you are required to set https-proxy, which can be done with the following command:
npm config set https-proxy http://proxy.example.com:8080

Resources