How to retrieve data from API using nodejs coffeescript - node.js

I have an API and i want to retrieve API data.I am using nodeJS and
coffeescript.How can i write nodejs script using coffeescript.Here is
my API and want to retrieve data from that API.Please help me.
http://apiprod.yourstory.com/v1/site/YOURSTORY/articles/

Please try to install request package using npm and use below code :
var request = require('request');
//Lets try to make a HTTP GET request to modulus.io's website.
request('http://www.modulus.io', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body); // Show the HTML for the Modulus homepage.
}
});
Below link explains in more detail :
http://blog.modulus.io/node.js-tutorial-how-to-use-request-module

Related

WOPI : File content is not displaying in DOC Viewer

I have implemented WOPI in my Vue.js app and implemented a GET API in Node.js to return the file content, still iam unable to see the content in DOC viewer in front-end and there are no errors even, attached screenshot of UI here:
UI DOC Viewer
The following is the action URL sample that i tried.
https://ffc-word-view.officeapps.live.com/wv/wordviewerframe.aspx?wopisrc=https://app.maindomain.com/file/v1/wopi/files/1269474
The following is the API Code which i wrote to read a file from Azure Blob storage and to return the file content:
var request = require('request');
request.get('<FILE_BLOB_URL>',{responseType: 'arraybuffer'}, function (error, response, body) {
if (!error && response.statusCode == 200) {
let json = JSON.stringify(body);
let bufferOriginal = Buffer.from(JSON.parse(json));
res.status(200).send(bufferOriginal)
}else{
console.log('WOPI Files Content Read Err is:');
console.log(error);
}
});
Can anyone suggest me the that the way that iam returning the file content is correct or not?
I have implemented '/wopi/files/:FID' API only for time-being, to get this working do i need to implement other endpoints also which were proposed in CSPP document?
To get the View mode working, you need at least:
CheckFileInfo
GetFile
To support the Edit mode, you need:
PutFile
*Lock - all locking operations

How to get data from api on html view using node js?

var Request = require("request");
Request.get("https://www.yonline.com", (error, response, body) => {
if(error) {
return console.dir(error);
}
document.getElementById('msg').innerHTML=JSON.parse(body)
console.dir(JSON.parse(body));
});
I tried this code, but it is only giving data on the command line. i want it to give data on the intended textfield.
NodeJS is server side script you can't access HTML element like this.
You need to do it on html javascript side.

How to call own API for input of other API in nodejs

i have a API called /validate which returns true or false value . I want true/false reponse to use in other API in nodejs .Please help
app.get('/validate',new TestController())
above api will give response like true or false
now in below api i want to call /validate and according to reponse i want to send result to user
app.get('/check',new Validator())
i used require('request') but getting error not found
I know this question is more than one year old, but:
The easiest way I found to consume data from your own self-hosted API is:
router.get('/', function(req, res, next) {
/* Find own IP and Port */
require('dns').lookup(require('os').hostname(), function (error, address) {
/* Assemble API URL (yours might be a little different) */
ownAPI = "http://" + address + ":" + req.app.get('port') + "/api";
/* Request from your own API */
request(ownAPI, function(error, response, body) {
// Whatever you wanna do here
If you have a successful api server, for example if you go into your browser and go to url/validate and it gives you a response (true or false or whatever in your case) then your request using the module "request" is incorrect.
In this case, your api endpoint uses GET to request. ~(someone edit me, I'm not good at english =P)
request.get('/validate', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body); // body is either 'true' or 'false'
}
});

596 Service not found while using ESPN API

I am using this API to consume a ESPN API:
http://api.espn.com/v1/now?apikey=d4skkma8kt2ac8tqbusz38w6
In Node.js, using node-curl library, my snippet looks like this:
var Curl = require('node-curl/lib/Curl')
curl.setopt('URL', url);
curl.setopt('CONNECTTIMEOUT', 2);
curl.on('data', function(chunk) {
console.log(chunk);
});
But everytime when I run this code I keep on getting response as:
<h1>596 Service Not Found</h1>
Strange is, same URL if I hit from the browser I get the correct response so URL is not invalid. This happens only if I try to call from Node.js. Can anyone guide me how to resolve this error? I tried encoding/decoding url, still I get same response.
Also basically I am avoiding any vendor specific libraries as much as possible, since we should have generic api calling framework
You could use request, which is a very popular module. Here's an example of how you could do it:
var request = require('request');
var url = 'http://api.espn.com/v1/now?apikey=d4skkma8kt2ac8tqbusz38w6';
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(JSON.parse(body));
}
});

Reddit API only returning one post

I am trying to get all of the links in a subreddit using the API, but it is only returning one url. Here is the code I have:
var request = require('request');
webpage = 'http://www.reddit.com/r/AmazonUnder5/top.json?limit=100';
//login
request.post('http://www.reddit.com/api/login',{form:{api_type:'json', passwd:'password', rem:true, user:'username'}});
//get urls
request({uri : webpage, json:true, headers:{useragent: 'mybot v. 0.0.1'}}, function(error, response, body) {
if(!error && response.statusCode == 200) {
for(var key in body.data.children) {
var url = body.data.children[key].data.url;
console.log(url);
}
}
});
When I visit the json link in my browser, it returns all 100 posts.
Thats because only 1 exists in the top
http://www.reddit.com/r/AmazonUnder5/top
You could use hot instead
http://www.reddit.com/r/AmazonUnder5/hot.json
Also, you don't need to log in to do public get requests
Edit: You are getting so few results because you are not logged in properly
When logging in, use the
"op" => "login"
Parameter and test what cookies and data is returned.
I also recommend using the ssl login url since that works for me
https://ssl.reddit.com/api/login/

Resources