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!
Related
A similar question is already asked, but the suggested answer requires request.post, but "request" is now deprecated. There is no suggested on alternative methods.
HTTP POST Google Cloud Functions NodeJS
Problem:
I've been looking for this for two days. I am simply looking for a bit of example code to send a POST request from a Google Cloud Function. I will pass a small bit of data and an auth token, but can't figure out the correct way to do it.
I will hit the API via HTTPS url and pass along the following parameters:
-d arg="1234" (a string)
-d access_token=0809809cx089009xci3 (an access token)
There are a million docs on how to trigger a cloud function from a POST, but nothing on how to actually generate the POST from within the cloud function.
Thanks in advance!
You can use libraries other than "request", for example: got, axios, or the default "HTTP" module in the standard node library. Checkout 5 Ways to Make HTTP Requests in Node.js.
Here is an implementation using "got":
const got = require('got');
const searchParams = new URLSearchParams([
['arg', '1234'],
['access_token', '0809809cx089009xci3'],
]);
got.post('https://example.com', { searchParams });
We use axios for http requests such as get, post, etc.
We use express for the same purpose also.
However according to what I read, they are for different purposes.
Please explain how.
PS: If you explain it by giving an example, it would be great!
You can think of express.js as a warehouse:
app.get('/item/:name', async function (req, res) {
res.send(await findItemByName(req.params.name));
});
If you want to get an item, for example a pencil, from this warehouse, you can use axios.js.
axios.get('/item/pencil')
Axios is used to send a web request whereas express is used to listen and serve these web requests.
In simple words, express is used to respond to the web requests sent by axios.
If you know about the fetch() method in javascript, axios is just an alternative to fetch().
I would say that express is used to create HTTP servers. So the server runs somewhere and responds to a request.
Axios is an HTTP client. It creates requests!
In very simple words axios is just passing the web request to the server-side (express). They basically work together (axios -> express -> DB)
I have a simple REST API written in Golang. Now I want to have an async UI and wanted to go with ReactJS + Babel or Typescript + fetch() to get data from my JSON api. Don't want nodejs or anything else.
Most of the examples require nodejs and I can't find something to have as an example to build the UI. Could someone help with an example?
Figured it out finally.
Basically using babel-standalone helped a lot. And using axios for fetching data was easy enough.
I have a Node Js application and I'm integration-testing my app with supertest/superagent + nockjs.
I have a problem, because I want to test my login rest apis using supertest to REPLY with a FAKE PROFILE RESPONSE + token for example for facebook/google/github and so on. (I'm not interested in LocalStrategy, because it' very simple)
How can I do that?
I'm trying with GitHub, and I wrote this code (not working) absolutely wrong, probably very stupid without any sense...It was an experiment XD.
nock('https://github.com/login/oauth')
.get('/authorize?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fapi%2Fauth%2Fgithub%2Fcallback&scope=user%3Aemail&client_id=XXXXXXXXXXXXXXXXXXXX')
.reply(302,undefined,
{
location : "http://localhost:3000/api/auth/github/callback?code=ab7f9823f03071209b26"
}
)
.get('http://localhost:3000/api/auth/github/callback?code=ab7f9823f03071209b26')
.reply(200, responseMocked);
PS: probably I made a mistake with url and status, I don't know.
Also, where I should set the connection.sid's cookie ?
How can I fix/rewrite this code to be able to integration-testing my application?
I'm also interested to use passportjs stub/mock, but I want a library supported and well documented.
UPDATE: I fixed the name of the mocked profile object (responseMocked)
Thank you,
Stefano.
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').