How to call 3rd party api like Gandi.Net from Node (server.js) - node.js

I am new to Node.Js I am creating an Mean Stack application, in which I want to call 3rd party API Gandi.Net from My Node.js code.
My Node.Js plus Express Application is getting use to make rest based API which is getting consume by my Angular client.
I found a little bit help from this link https://github.com/baalexander/node-xmlrpc. ut not so much.
Do I need to make new server for XML-RPC?
If any one have did this kind of work, then any sample application will help a lot.
If any one have made any http call from node application then sample application will help.

It's quite easy to make an http call from the node server.
You can use the request module.
The documentation has tons of examples as well.
A very simple example from the module itself
var request = require('request');
request('http://www.google.com',
function (error, response, body
{
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});

Related

NodeJs Express how to handle items(params) that sent from frontend?

Im new in backend development (using NodeJs Express).
Its very basic question (I didn't find any good tutorial about it)
Question is:
I have this line of code:
app.get('/test', function (req ,res){
res.send('test');
});
What I wanna do is: BackEnd only sends res to FrontEnd, if FrontEnd send some JSON first.
Like Backend will show Something to FrontEnd, only if FrontEnd send JSON first;
How to handle it? What code to write?
Or what to type in google search to find this kind of tutorial
You are building a REST API with node. In REST we don't keep states. When we receive a request we process and respond. In the Front end, you can do wait until the response is received. use promises, async-await or callbacks to wait until the response in the Front end. Use these methods to connect with back end from front-end axios, fetch. To process the incoming JSON body use body-parser. Based on the request body you can process and send the response. PS: Every request should be given a response. That's how REST behaves.
In Query
yourbackend.com/test?message=welcomeToStackOverflow
This is how you can access with in query:
const {message} = req.query;
console.log(message);
// welcomeToStackOverflow
In Params
yourbackend.com/test/:message
This is how you can access with in params :
const {message} = req.params;
console.log(message);
// welcomeToStackOverflow
Here you have working example : https://codesandbox.io/s/trusting-rosalind-37brf?file=/routes/test.js

Unit testing MobileFirst Platform Java Adapters with OAuth using NodeJS

I am writing a NodeJS application to test the IBM MobileFirst Platform adapters that I have written. The approach I want to follow to do this, as follows:
Get a test token from the http://localhost:10080/AppName/authorization/v1/testtoken
Use this Bearer token to make authenticated requests to my protected adapters.
The problem with approach is that, when I try to make a request to the testtoken end point, I am getting an HTTP 405 status error. However, the same works with PostMan.
Is there a way to make this work in the NodeJS application? I am using Request to send requests to the MobileFirst Server.
I am writing my NodeJS application using SailsJs.
The test token operation requires POST request.
request.post('http://localhost:10080/app/authorization/v1/testtoken', function(error, response, body) {
console.log(response.statusCode);
if(!error && response.statusCode == 200) {
console.log(body);
return res.ok(body);
} else {
return res.notFound(error);
}
});
In my case, I was using Request so the above code worked.

Context IO node.js client library

I'm using Context.io's Node.js client library to get the body of emails from my account which is done in my code below.
Once I have that data, I am confused as to how I would get them over to the front-end. I'm new to the whole MEAN stack, and from my research of the basics, I have gathered that with Node, you can make the http calls, parse the json response and then write it to the server, but it seems as if I would have to make an API to then be able to call it from Angular and display it how I wish. I know that with other back-end languages like PHP, you can just write the code within the html, and not need a middle structure to get the info from PHP to the front-end. Is there an easier way than writing my own API in Node after having already made an API request to Context.io?
ctxioClient.accounts(ID).messages().get({limit:10}, function ( err, response) {
if(err) throw err;
console.log("getting responses...");
var messages = response.body;
console.log("message id is " );
console.log(messages.body);
You should implement the backend in JSON using something like res.json and make AJAX requests via Angular $http or something similar.

NodeJS - Go to a site without actually visiting it?

Okay so what I am trying to achieve here is that I have made an PHP script with a bunch of $_GET's.
So I would like to go to a website without actually visiting it with NodeJS. Basically to trigger this script with parameters.
I hope that explains what I am trying to do. I really have no idea how to make it trigger a site or whatever you can call it - hence why not posting anything that I have tried before posting this.
Thanks in advance.
To make an HTTP request from node, you may use the standard http package. You'll find examples in the standard documentation.
To make them in a simpler way, you may also use the request module. Using it, a http request is as simple as that :
var request = require('request');
request("https://yourwebsite.com/page.php?a=b", function(error, res, body){
if (error) console.log("error:", error);
else console.log('body of the response :', body);
})

build multiple layer restful API in node.js express

I am using node.js express to build simple rest API, I had built a API like
app.get('/sites/:site/:building/:floor',function(req,res){
var building = req.params.building, floor = req.params.floor, site = req.params.site;
console.log('query site ',site,building, floor);
.....
}
when client did the AJAX request in angular.js
$http.get('/sites/london')
.success(function(data) {
}).error(function(data, status, headers, config) {
});
the server doesn't respond to this kind of URL, unless I changed the express API to
app.get('sites/:site',function(){})
app.get('sites/:site/:building',function(){})
app.get('sites/:site/:building/:floor',function(){})
But the way above is too tedious so I wonder if it is possible to build API in one sentence app.get('sites/:site/:building/:floor',function(){})
The following stackoverflow answer should help you out. But to answer your question, the below should work
app.get('sites/:site?/:building?/:floor?',function(){})

Resources