Getting response from a website using Node.js - node.js

I am making a simple code in node using request to test if a website exist or not.
I want response if website exists with statusCode 200, if not then exit with statusCode 404
Here is my stuff
var http = require('http');
var request = require('request');
http.createServer(function (req , res) {
res.write("hello");
request('http://www.cutm.ac.in', function (error , response , body) {
if(!error && response == 200){
console.log(body);
}
else{
console.log("hdsa");
}
});
}).listen(9000);
console.log("started");
If I am doing
if(!error && response == 200)
it's not working, but when I remove response == 200, I am getting the html codes.
Any help

Try this.
var http = require('http');
var request = require('request');
http.createServer(function (req , res) {
res.write("hello");
request('http://www.cutm.ac.in', function (error , response , body) {
if(!error && response.statusCode == 200){
console.log(body);
}
else{
console.log("hdsa");
}
});
}).listen(9000);
console.log("started");
Always check statusCode to get response status

Related

How to call rest api in express js

I am new in nodejs and I am using express js, I am calling REST API and want to show the response, but my problem is that the response is showing in console.log but I want to pass the body(response) in assistant.ask, where I am wrong here is my code:
var request = require('request');
let rawInput = function (assistant) {
let rawInput = assistant.getRawInput();
request.post(
'http://xxxxxxx.ngrok.io/api/v1/240/respond',
{ json: { query: rawInput } },
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
assistant.ask(body);
}
else{
console.log(error);
}
}
);

How to make a post request using request module NodeJs express

How to make a proper post request to this endpoint. When I use the POSTMAN I get the correct response but when I call using the below function I get 503 error. The call seems to be fine according to me. I appreciate your help!!
const request = require('request');
const express = require('express');
// Initialize request
var img64Data = "/9j/4AAQSkZJRgABAQAAAQABAAD/2w… "; // Include the entire base64 encoding. // Shown Below in the next page
var send = {"img64": img64Data};
var api_address = "https://8n78hbwks0.execute-api.us-west-2.amazonaws.com/dev/";
// Make Post Request
module.exports = app => {
app.post('/axe', (req, res, next) => {
console.log("inside the axe");
request.post({
url: api_address,
body: JSON.stringify(send),
headers: {"Content-Type":"application/json"}
}, function (error, response, body) {
console.log("hiii");
console.log(response.statusCode);
if (!error && response.statusCode == 200) {
// Successful call
var results = JSON.parse(body);
console.log(results) // View Results
}
});
});
};
You get a 503 Error https://en.wikipedia.org/wiki/List_of_HTTP_status_codes because your server doesn't reply any http code.
if (!error && response.statusCode == 200) {
// Successful call
var results = JSON.parse(body);
console.log(results) // View Results
res.sendStatus(200);
} else {
res.sendStatus(response.statusCode);
}

How to make response of request module nodejs to object?

I'm using request npm module
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage.
}
})
my response return format such as "string". How to convert response to object?
I found answer
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info ) // Show the HTML for the Google homepage.
}
})

How to get response headers when using needle.js in streaming mode?

I want to use the needle module for node.js in streaming mode, similar to this example from the needle docs:
var stream = needle.get('http://www.as35662.net/100.log');
stream.on('readable', function() {
var chunk;
while (chunk = this.read()) {
console.log('got data: ', chunk);
}
});
This allows me to read the response body from the stream.
How can I access the response headers?
From reading the source, needle emits two events, header and headers.
Interested in headers only:
var stream = needle.get(someURL);
stream.on('headers', function(headers) {
// do something with the headers
});
or status code and headers:
stream.on('header', function(statusCode, headers) {
if (statusCode != 200) {
// scream and panic
}
});
You can read the header before stream starts if you want to.
var needle = require('needle');
var url = 'http://www.stackoverflow.com';
needle.head(url, {method: 'HEAD'}, function (err, response) {
if (!err && response.statusCode == 200) {
console.log((JSON.stringify(response.headers)));
}
});
Or in Request
var request = require('request');
var url = 'http://www.stackoverflow.com';
request(url, {method: 'HEAD'}, function (err, response) {
if (!err && response.statusCode == 200) {
console.log((JSON.stringify(response.headers)));
}
});
Otherwise you can read it after stream.
var needle = require('needle');
var url = 'http://www.stackoverflow.com';
var stream = needle.get(url, function (err, response) {
if (!err && response.statusCode == 200)
console.log((JSON.stringify(response.headers)));
});
But this is also valid for request.
var request = require('request');
var url = 'http://www.stackoverflow.com';
var stream = request.get(url, function (err, response) {
if (!err && response.statusCode == 200)
console.log((JSON.stringify(response.headers)));
});

How to render the results of an http request in Express?

Using Request and Express, how do I access the result of my http request for the purpose of rendering it?
var request = require('request');
var http = require('http');
exports.index = function(req, res){
var apiUrl = 'http://api.bitcoincharts.com/v1/weighted_prices.json';
request(apiUrl, function(err, res, data) {
if (!err && res.statusCode == 200) {
data = JSON.parse(data);
console.log(data);
res.render('index', { data: data });
}
});
};
As it is, the res I'm referring to within the request callback is the raw response object and I'm wondering how to call the response from my exports.index function without the request being inaccessible.
Just rename one of the arguments:
// either this:
exports.index = function(req, response) {
...
response.render(...);
};
// or this:
request(apiUrl, function(err, response, data) {
if (!err && response.statusCode == 200) {
data = JSON.parse(data);
console.log(data);
res.render('index', { data: data });
}
};

Resources