I have created a get web api that i want to hit at my server startup for some specific task. I have used this method
var request = require('request');
request('http://localhost:3000/api/jobs/asd', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Print the google web page.
}
}
)
But i am getting status code as 403. So i am not able to access my API at serve startup. Please suggest me the proper solution for the same.
thanks in adv.
You can place your code inside http callback like so :
http.createServer(app).listen(3000, () => {
//your code here
});
Related
I am trying to access Firebase Database using REST API. Using cURL i am able to retrive data. I am unable to do the same in javascript.
cURL
curl 'https://testproject123.firebaseio.com/subscription.json?auth=aabbccddeeff123'
Javascript
var request = require('request');
var options = {
url: 'https://testproject123.firebaseio.com/subscription.json?auth=aabbccddeeff123' };
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
} }
request(options, callback);
Note: The above code i am trying to run on Bluemix/OpenWhisk.
Kindly let me know how to fix this.
The structure of your action is a bit off for OpenWhisk. Javascript actions need to have a main method and you'll have to use whisk.async() and whisk.done() to make your (asynchronous) REST API call work as you'd expect it.
An example of how to call an external API using a Javascript action in OpenWhisk can be found here.
I should preface my post by saying that I am a beginner and this is my first time using Node.js and Express in a real project.
I have a simple Node.js/Express project and I want to read a JSON object from a URL. Afterwards, I intend to build another url that displays html from an external website using iframe.
I read about the 'request' module online and know that I need to do something along these lines:
var express = require('express');
var router = express.Router();
var request = require('request');
// Urls for App Center REST functions
var url = 'https://someserver.com/appserver/portal/api/1.0/results/recent';
/* GET list of recent reports */
router.get('/testapi', function(req, res, next) {
res.render('testapi', { title: 'List Recent Reports' });
});
/* TEST: function to GET report list */
router.get('/recentreports', function(req, res){
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body) // Print the json response
}
})
});
I have tried to define a function /recentreports which is called in the testapi.jade view, however nothing is printed in the console when I load the page and I suspect I am doing something horribly wrong.
My questions are:
How do I read the JSON into my app and where does this code go (index.js, app.js, testview.jade etc...?)
How do I export the URL I construct from wherever that code lives to my .jade view?
There was nothing logged to the browser console because no response was sent from your server. The response was only logged to the server's console.
You'll need to refactor the code for the 'recentreports' route to send data. You could use a simple res.send call:
...
function (error, response, body) {
if (!error && response.statusCode === 200) {
res.send(body) // Send the response to the client
}
}
...
This response will be received by testapi.jade via an AJAX call to the '/recentreports' route. The AJAX call can be defined in a Javascript file sourced by the testapi.jade file.
The constructed URL would not need to be exported as it exists within the same testapi.jade file (after you've formed it from the results from the AJAX call).
I am trying to implement the DuckDuckGo Instant Answer Api into my NodeJS application.
For that I request data from the Api using Node Request.
var request = require('request');
request('http://api.duckduckgo.com/?q=Hamburg&format=json&pretty=1', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(JSON.parse(body));
}
})
This gives me the result in English, but I would like to have it in German. In my browser it is in German. How can I change my code so I can get the data in German in NodeJS?
Thank you!
You can add the kad key with a proper locale.
As so:
http://api.duckduckgo.com/?q=Hamburg&format=json&pretty=1&kad=de_DE
I am using the request module in node to make an http request. How do I read the response content of that request so that I can make a programmatic decision based on the response?
gulp.task("run-server-tests", function(){
var responseJson = request("http://myurl/run-server-tests")
plugins.util.log(responseJson); // not sure how to get at the response json
});
request allows you to use callback function, see more detail
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage.
}
})
I have this system
An API system which only response with JSON objects.
Example: http://example.com/user/13
response: {name:'Aesome 1', age: '13'}
ExpressJS web app which creates the views and sends the views to the user.
Now what I need to do it to get the JSON object from the API and render a view in ExpressJS and then send to the client.
So I need to connect the ExpressJS app with this api system.
Please let me know how to do this.
Thanks
You can use the request module for making api requests.
In your controller, do like this:
var request = require('request');
function(req, res) {
request.get('http://example.com/user/13', function(err, response, body) {
if (!err && response.statusCode == 200) {
var locals = JSON.parse(body);
res.render('<YOUR TEMPLATE>', locals);
}
}
}
Note: If you really want to access api from server then use the sample, else you can fetch the result using ajax with less overhead of another server to server http call.