How can I pass extra options to Node in Meteor's HTTP.call()? - node.js

I'm getting an SSL error when doing an HTTP.get() call in Meteor, UNABLE_TO_VERIFY_LEAF_SIGNATURE.
The links above point to solutions involving Node parameters (for instance {rejectUnauthorized: false}), but it's unclear how to pass any of those to Meteor. I've tried HTTP.get(url, {rejectUnauthorized: false}) without luck.

It's now possible by passing npmRequestOptions to Meteor HTTP requests:
const requestOptions = {
npmRequestOptions: {
rejectUnauthorized: false
}
}
const result = HTTP.get(url, requestOptions)

I ended up creating a fork of Meteor's HTTP package, which just passes through options it doesn't know about. I think it's a sane thing to do (instead of discarding the options entirely), and I hope the Meteor team pulls the change into core.
The Atmosphere package is called http-more.

Looking at the source of the HTTP package (https://github.com/meteor/meteor/blob/devel/packages/http/httpcall_server.js#L75), I noticed that it isn't implemented using node's http class directly, but instead uses the request package and the options you can pass it (see line in above link) are limited. So I'm not sure this is currently possible.
Looking at the request package's request options (https://github.com/mikeal/request#requestoptions-callback) I wouldn't be sure how to enable the option you care about either.
BTW, if you are on the server, you can always use http(s) directly using Npm.require('https').

Related

fastify-swagger is not picking up my dynamic routes

I've been a fan of ExpressJs for a long time but in a Youtube video I stumble upon Fastify and wanted to give it a try
I'm struggling in making the fastify-swagger plugin work as I assume it should work - dynamic setup to pick up the schema from each route, but I'm certainly missing something 😔
here's my test repo that after running, none of my routes appear
my setup for the plugin is the default one
but all I see is
I've read in the read me that because of OpenAPI specs, some properties, like description are mandatory or will not pick up the route, but I've added in one route, and still does not pick up, I've also added tags wondering if that was also mandatory, but nothing...
does anyone know what am I missing? must be a simple thing, but got me puzzled this last few days 😔
I ran into the same issue and ended up solving it by following the first Usage example line-by-line: https://github.com/fastify/fastify-swagger#usage
const fastify = require('fastify')()
(async () => {
// set up swagger
await fastify.register(require('#fastify/swagger'), {
...swagger config
});
// define all your routes
// then call these
await fastify.ready()
fastify.swagger()
})();
Consider the order in which your plugins are loaded, the routes need to be registered before fastify swagger. If fastify swagger comes first, it doesn't detect any route.
I encountered this issue in my project. In my case, I solved it using fastify-plugin. Looking at the source code for fastify-swagger, it seems to rely on a hook listening for onRoute events to detect routes. I'm thinking maybe encapsulation can interfere with the plugin's ability to receive the events.

How do you include an SSL certificate in an API request in Node?

Here is the situation: I have a certificate, key, public key, and private key. I am building an app that integrates with another system. My system is a Next.js app running on Azure. It makes API requests to an external server running somewhere else. I know that my certificate/keys are good because the request works when I use the Insomnia API Client (200 response and data coming back).
However, trying to do this in Node, I keep getting 400 errors. I have tried doing this with core fetch and https functionalities and using the node-fetch package. I also looked at the request package (as there are examples of this on the internet), but that package was deprecated over a year ago, so I think it should not be used.
Here's an example of some of the code I've tried using. I'm storing the certs/keys in /tmp/certs on my local system & they are loading (I've been logging heavily). I've used a wide variety of different requestOptions, but nothing seems to be doing the trick.
import fs from 'fs'
import fetch from 'node-fetch'
const requestOptions = {
method: 'GET',
port: 443,
cert: fs.readFileSync("/tmp/certs/ct.crt", 'utf-8'),
key: fs.readFileSync("/tmp/certs/ct.key", 'utf-8')
};
const res = await fetch("https://url.com?unique_ID=1234&key=abcd", requestOptions);
Examining the res always turns up a 400. I have tried doing this a number of different ways at this point. This seems like it should be a trivial, common use case... What am I doing wrong and how should I be structuring this request?
Help me, Stack Overflow, you're my only hope!

A library for nodejs connection with backend

i am creating a node application which will use a another node backend, like a CLI program, which Node.js library or framework will i use for this situation? the axios not is working in my tests.
thanks for reading.
If you want to do http requests from within node.js there are several options: https://www.npmjs.com/package/node-fetch, axios is also available. Could you please share some errors you get?
I always use node-fetch because I am most familiar with the fetch API, previously I used this package: https://www.npmjs.com/package/xmlhttprequest
As seen in your answer you are using /users as url, please use the full url. Also catch any unexpected errors.
So axios.get('http://domain/users')
I SOLVED THE PROBLEM!
the problem was in the axios connection, now its:
async function execQuery(apps){
data = ({
name: "ederson",
apps: "neofetch"
})
const response = await axios.put('http://localhost:3333/users', data
).then(console.log(apps))
}
Thanks for help me, Laurent Dhont and Ahmed Hammad!

Check the protocol of an external URL in NodeJS

Is there a way to check what the protocol is of an external site using NodeJS.
For example, for the purposes of URL shortening, people can provide a url, if they omit http or https, I'd check which it should be and add it.
I know I can just redirect users without the protocol, but I'm just curious if there is a way to check it.
Sure can. First install request-promise and its dependency, request:
npm install request request-promise
Now we can write an async function to take a URL that might be missing its protocol and, if necessary, add it:
const rq = require('request-promise');
async function completeProtocol(url) {
if (url.match(/^https?:/)) {
// fine the way it is
return url;
}
// https is preferred
try {
await rq(`https://${url}`, { method: 'HEAD' });
// We got it, that's all we need to know
return `https://${url}`;
} catch (e) {
return `http://${url}`;
}
}
Bear in mind that making requests like this could take up resources on your server particularly if someone spams a lot of these. You can mitigate that by passing timeout: 2000 as an option when calling rq.
Also consider only requesting the home page of the site, parsing off the rest of the URL, to mitigate the risk that this will be abused in some way. The protocol should be the same for the entire site.

Why is config.proxy ignored when making an axios request within a webpack project?

My goal
I want to perform a request with axios#0.18.0 using an http proxy fully efficient (squid). My project is a vue project based on the webpack template. vue init webpack proxytest
The issue
When I try to perform the request, axios 'ignores' the proxy property inside the config object passed.
I noticed that when I run the exact same code with pure nodejs, everything works just perfectly fine.
Is there some configuration that I need to specify excepting the axios request configuration when using axios as a npm module within webpack ?
The code
import axios from 'axios';
const config = {
proxy: {
host: 'host',
port: 3128,
},
method: 'GET',
};
axios('http://www.bbc.com/', config).then((res) => {
console.log(res);
}).catch((err) => {
console.error(err);
});
Of course, when testing, I change 'host' into the proxy IP.
I tried to change the method property to POST in order to check if axios considered the config. It does consider the config passed.
I tried to put a fake port so I could check if the proxy property was considered. It's not considered.
The output
output...
Now, I'm aware of what CORS is. The point is I'm constently getting this output when performing the requests. And if the proxy was used by axios, I think no CORS "error" would show up as my proxy is a VPS.
Thank you.
You need to configure your server to receive the requests and then test. This does not seem to have anything to do with the webpack, because in your mistake, for example, you make a request for the BBC from localhost and it is very likely that you are making that mistake. So it's important to test with your server by running Front and Back locally.

Resources