Http request does not seem to work as it should with iisnode ...
When I run this without running it under IIS work it just fine ...
Anyone know if I need to give any permission or something, or someone who has experience in general with http request to other domain using IIS Node.
I can unfortunately not show debug, since I have not gotten it to work real under IISNode.
var http = require('http');
var cached_defs = [];
function loadArticleDef() {
var options = {
host: 'http://someurl.com',
path: '/data/DataObjectDef/GetDef?ArticeID=mypage'
};
callback = function(response) {
response.on('data', function (chunk) {
cached_defs.push(JSON.parse(chunk));
});
response.on('end', function () {
console.log("end");
});
}
http.request(options, callback).end();
}
Related
I have found very simple code that creates a server and displays a string, using the "http" module.
eg. from https://garywoodfine.com/simple-lightweight-nodejs-webserver/:
var http = require('http');
var server = http.createServer(function (req, res) {
var body = 'Amazing lightweight webserver using node.js\n';
var content_length = body.length;
res.writeHead(200, {
'Content-Length': content_length,
'Content-Type': 'text/plain' });
res.end(body);
});
server.listen(3939);
console.log('Server is running on port 3939');
I have found very simple code that gets data over HTTP, using the "got" module.
eg. from https://nodesource.com/blog/express-going-into-maintenance-mode:
const got = require('got');
(async () => {
try {
const response = await got('https://www.nodesource.com/');
console.log(response.body);
//=> '<!doctype html> ...'
} catch (error) {
console.log(error.response.body);
//=> 'Internal server error ...'
}
})();
However I am failing to integrate the two to create a server that, when visited, makes the HTTP call and returns the result. I essentially just want to replace the var body = 'Amazing lightweight webserver using node.js\n'; line from the Gary Woodfine example with the output of the Nodesource example.
I'm not particularly looking for comments or questions as to why I would want to make something that does this, I'm trying to understand fundamentally why I can't just do what feels like a very simple and natural thing to do: return content based on a server side request to another web service. I get the impression that the issue is to do with the asynchronous paradigm and obviously I understand the performance improvements it offers, I'm failing to understand how you structure something that works for this simple usecase.
With thanks to Brad for his comment, I now have code that integrates the two samples:
var http = require('http');
const got = require('got');
var server = http.createServer(function (req, res) {
var body = (async () => {
try {
const response = await got('https://www.nodesource.com/');
var body = response.body;
var content_length = body.length;
res.writeHead(200, {
'Content-Length': content_length,
'Content-Type': 'text/plain' });
res.end(body);
//=> '<!doctype html> ...'
} catch (error) {
console.log(error.response.body);
//=> 'Internal server error ...'
}
})();
});
server.listen(3939);
console.log('Server is running on port 3939');
This code can be stripped down further obviously, to the sort of level of simplicity I had in mind.
Where I was going wrong was by trying to handle all the http response code after the async block, when I needed to do it inside it.
I am running a api on http://localhost:80/api/test, I want to invoke this api from lambda function but I am not sure what plugin or anything I should use to access same.The reason for for doing so is, I want to do testing lambda and api in development phase
I used https://ngrok.com/ to solve it.
Below is the command for localhost https urls. You can replace the port no 3000 with your port no.
ngrok http https://localhost:3000 -host-header="localhost:3000"
Below is my Lambda Function:
var https = require('https');
exports.testJob = (event, context, callback) => {
var params = {
host: "90abcdef.ngrok.io",
path: "/api/test"
};
var req = https.request(params, function(res) {
let data = '';
console.log('STATUS: ' + res.statusCode);
res.setEncoding('utf8');
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
console.log("DONE");
console.log(JSON.parse(data));
});
});
req.end();
};
I am encountering the Nodejs error ECONNREFUSED 127.0.0.1:3000 when I try to send a get request to load a static file. I have seen many questions on this error but apparently there is no straight answer as to why this error is thrown. Here below is the code I have. I have tried changing localhost to 127.0.0.1 or change port numbers 3000, 7000, 8080 but nothing resolved it. Can someone please advised? Thank you.
//Basic web client retrieving content of file using get request
var http = require('http');
//options for request
var options = {
hostname: 'localhost',
port: '3000',
path: '../html/hello.html'
};
//function to handle response from request
function getResponse(response){
var serverData='';
response.on('data', function(chunk){
serverData += chunk;
});
response.on('end', function(){
console.log(serverData);
});
};
http.request(options, function(response, error){
getResponse(response);
}).end();
Your client code is good enough to work. You are getting the ECONNREFUSED because there is a no server which is listening to the particular port number and the Server is not sending any data and you are requesting to get data from the server and accumulate it.
Here is the sample code:
//Requesting for http and fs modules
var http = require('http');
var fs = require('fs');
//creating the server and reading the file synchronously
var server = http.createServer(function(req, res) {
res.end(fs.readFileSync('./html1.html'));
}).listen(3000);
//creating the client which connects to local host at port number 3000
var options = {
hostname: 'localhost',
port: '3000',
}
//getting the data from server, storing it in to a variable, and printing at end of the data
function getResponse(response){
var serverData='';
response.on('data', function(chunk){
serverData += chunk;
});
response.on('end', function(){
console.log(serverData);
});
};
http.request(options, function(response, error){
getResponse(response);
}).end();
the issue in your code , that you are not handling error, for this you have to handle error first.
var http = require('http');
var options = {
hostname: 'localhost',
port: '3000',
path: '../html/hello.html'
};
function getResponse(response){
var serverData='';
response.on('data', function(chunk){
serverData += chunk;
});
response.on('end', function(){
console.log(serverData);
});
};
http.request(options, function(error , response){
if(error){
console.log(error);//handle error here.
}else{
getResponse(response);
}}).end();
New to node.js and Lambda. I am working on an Alexa Skill. I have a single string stored in a file on a web server.
I am looking for an example of code that does an HTTP request and returns the value.
Example web server at : http://www.shafer.ca/percent
Seems like it should be very easy, but I cannot seem to find an example.
You can make a basic http get request, which gets some data from the server.
var http = require("http");
var options = {
"method": "GET",
"hostname": "localhost",
"port": "3000",
"path": "/",
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.end();
i hope this helps
When I do a simple http.get for a URL that goes to a SquareSpace (SS) site I'm getting a 403 message. I know the site is working and that the server can reach it. Here's a simple example against a SS site (not mine, but produces the same issue):
Show that server can access site:
curl http://www.letsmoveschools.org
This returns all the HTML from the site...
Node app
var http = require('http');
var url;
url = 'http://www.letsmoveschools.org/';
var req = http.get(url, function(res) {
res.on('data', function(chunk) {
//Handle chunk data
});
res.on('end', function() {
// parse xml
console.log(res.statusCode);
});
// or you can pipe the data to a parser
//res.pipe(dest);
});
req.on('error', function(err) {
// debug error
console.log('error');
});
When I run the node app now node app.js it outputs the 403 status code.
I have tried this code with other sites and it works fine, just not against squarespace sites. Any idea of either configuration on SS or something else I need to do in Node?
The problem is that the remote server is expecting/requiring a User-Agent header and node does not send such headers automatically. Add that and you should get back a 200 response:
// ...
url = 'http://www.letsmoveschools.org/';
var opts = require('url').parse(url);
opts.headers = {
'User-Agent': 'javascript'
};
var req = http.get(opts, function(res) {
// ...