I tried to access folder from my server path in my project but not working.I do not know where I did mistake or missing script.
var http = require('http');
//Actual path is http://proxy.ipt.org/power/confolder
var options = {
host: 'http://proxy.ipt.org',
port: 8081,
path: '/power/confolder'
};
http.get(options, function(resp){
resp.on('data', function(chunk){
console.log(chunk);
//How to read files and How to display what are the folders are there inside confolder
});
}).on("error", function(e){
console.log("Got error: " + e.message);
});
Getting Error: got error: getaddrinfo ENOTFOUND http://proxy.ipt.org/power/confolder
How to read confolder? If anyone know help me to resolve this issue.
I think you just have to remove 'http://' from options.host, so your options variable would be:
var options = {
host: 'proxy.ipt.org',
port: 8081,
path: '/power/confolder'
};
Of course if http://proxy.ipt.org:8081/power/confolder does not reply to get request or the host is not reachable/does not exist you will get an error again. To read confolder you would need the server to return the content in some way but that's another topic that goes beyond how to request the content which is what you are trying here.
Here is a working example with a server that works and returns content when receiving a get request:
var http = require('http');
var options = {
host: 'nodejs.org',
port: 80,
path: '/dist/index.json',
};
http.get(options, function(resp){
resp.on('data', function(chunk){
console.log(chunk);
});
}).on("error", function(e){
console.log("Got error: " + e.message);
});
Related
In my node.js app, I want to make an https api call. I am trying with the https module and it is not working, but then I try with a request module, and that works.
not work
var options = {
host : 'myserver/platform-api/v1',
port : 80,
path : '/projects?access_token=38472',
method : 'GET',
headers : {
'Accept' : 'application/json'
}
};
var req = https.request(options, function(res) {
res.on('data', function(chunk) {
console.log(chunk);
});
});
req.on('error', function(e) {
console.log(e);
console.log('problem with request:', e.message);
});
req.end();
I get this
problem with request: getaddrinfo ENOTFOUND myserver/platform-api/v1
myserver/platform-api/v1:80
this works
request("https://myserver/platform-api/v1/projects?access_token=38472", function(error, response, body) {
if (error) return console.log(error);
//console.log(error);
//console.log(response);
console.log(body);
});
I can't figure out why it does not work on the first one. Does anyone know why?
Thanks
EDIT Switched to port 443 as well.
Your host seemed to include part of the path? Try this instead (left just the host in host and moved the path to path):
var options = {
host : 'myserver',
port : 443,
path : '/platform-api/v1/projects?access_token=38472',
method : 'GET',
headers : {
'Accept' : 'application/json'
}
};
Also, you're hitting port 80, which is usually not the HTTPS port.
Following node js code works well and get all the required results.
But when i run the code in the computer under proxy settings defined. It doesnt give the result.
var options: https.RequestOptions = {
host: item.hostname,
port: +item.port,
path: item.path,
method: 'POST',
headers: newHeader
}
var req = https.request(options, function(res){
if(res.statusCode !== 200){
//reject();
//return;
}
var result = '';
res.setEncoding('utf8');
res.on('data', function(chunk){
result += chunk;
});
res.on('end', function(){
resolve(result);
});
res.on('error', function(e){
reject(e);
});
});
How to enable proxy support in http module requests in NODE JS that it checks if there is any proxy defined and perform that enabled request.
options data in debug mode :
headers:Object
Accept:"application/json;api-version=3.0-preview.1"
Content-Length:104
Content-Type:"application/json"
host:"marketplace.visualstudio.com"
method:"POST"
path:"/_apis/public/gallery/extensionquery"
port:0
The proxy should be the host/port and the path is the final path of the request that the proxy should make (host:port/path).
Basically e.g. if making a request through a proxy on localhost:8888:
path = host + path, host = 'localhost', port = 8888;
I'm using this code to make an HTTP request to my own server. I'm getting the appropriate response in chunk.
http.createServer(function (req, res) {
var options = {
host: '<my ip>',
port: 8080,
method: 'GET',
path: '/content?data='+somedata
};
var call = http.request(options, function(res){
res.setEncoding('utf8');
res.on('data', function(chunk){
console.log("got response"+chunk);
});
}).on("error", function(e){
console.log("Got error: " + e.message);
});
call.end();
}).listen(3000);
My question is how I can print this chunk to my browser?
Change one of your two res variables to have a different name. At the moment the response to your request is masking the response you are trying to make.
Then:
response.write(chunk);
See also: https://nodejs.org/api/http.html#http_response_write_chunk_encoding_callback
res.write(chunk);
also don't forget to end first call, so the browser will know the request has ended.
res.end("success");
I have a code like this:
var options = {
host: "https://basic:authentication#website.com",
path: "/api/address"
};
var request = https.get(options, function(response){
var str = "";
response.on('data', function(chunk){
str+=chunk;
});
response.on('end', function(){
console.log(str);
res.json(str);
});
});
request.end();
request.on('error', function(err){
console.log(err);
});
This gives me
{ [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }
I don't know what's wrong because if I change the request to look like this:
var request = https.get("https://basic:authentication#website.com/api/address", function(response){
It works and gets an answer from the api. The problem is that I can't input parameters into the call if I do it this way. Does anyone have tips?
The problem is that your host value is not correct, it should really just be the hostname. For the HTTP basic auth, you can use the auth setting. For example:
var options = {
host: "website.com",
path: "/api/address",
auth: "basic:authentication"
};
Also, explicitly calling request.end() is unnecessary since https.get() internally does that for you automatically.
I should also note that since it seems like you're responding to an existing request with a new, external request, you can simplify it further by simply piping the external response to the existing response:
https.get(options, function(response) {
// You should probably check `response.statusCode` first ...
res.set('Content-Type', 'application/json');
response.pipe(res);
}).on('error', function(err) {
// ...
});
I'm brand new to node.js, but I wanted to play around with some basic code and make a few requests. At the moment, I'm playing around with the OCW search (http://www.ocwsearch.com/), and I'm trying to make a few basic requests using their sample search request:
However, no matter what request I try to make (even if I just query google.com), it's returning me
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/0.7.65</center>
</body>
</html>
I'm not too sure what's going on. I've looked up nginx, but most questions asked about it seemed to be asked by people who were setting up their own servers. I've tried using an https request instead, but that returns an error 'ENOTFOUND'.
My code below:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
var options = {
host:'ocwsearch.com',
path:
'/api/v1/search.json?q=statistics&contact=http%3a%2f%2fwww.ocwsearch.com%2fabout/',
method: 'GET'
}
var req = http.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
Sorry if this is a really simple question, and thanks for any help you can give!
For me the website I was trying to GET was redirecting me to the secure protocol. So I changed
require('http');
to
require('https');
The problem is that Node.JS's HTTP Request module isn't following the redirect you are given.
See this question for more: How do you follow an HTTP Redirect in Node.js?
Basically, you can either look through the headers and handle the redirect yourself, or use one of the handful of modules for this. I've used the "request" library, and have had good luck with it myself. https://github.com/mikeal/request
var http = require('http');
var find_link = function(link, callback){
var root ='';
var f = function(link){
http.get(link, function(res) {
if (res.statusCode == 301) {
f(res.headers.location);
} else {
callback(link);
}
});
}
f(link, function(t){i(t,'*')});
}
find_link('http://somelink.com/mJLsASAK',function(link){
console.log(link);
});
function i(data){
console.log( require('util').inspect(data,{depth:null,colors:true}) )
}
This question is old now, but I got the same 301 error and these answers didn't actually help me to solve the problem.
I wrote the same code:
var options = {
hostname: 'google.com',
port: 80,
path: '/',
method: 'GET',
headers: {
'Content-Type': 'text/plain',
}
};
var http = require('http');
var req = http.request(options, function(res) {
console.log('STATUS:',res.statusCode);
console.log('HEADERS: ', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log(chunk);
});
res.on('end', function() {
console.log('No more data in response.');
});
});
req.on('error', function(e) {
console.log('problem with request: ', e.message);
});
console.log(req);
req.end();
so after some time I realized that there's a really tiny mistake in this code which is hostname part:
var options = {
hostname: 'google.com',
...
you have to add "www." before your URL to get html content, otherwise there would be 301 error.
var options = {
hostname: 'www.google.com',