Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending http request in node.js

Tags:

I am trying to send a http request to a neo4j database using node.js. This is the code I am using:

var options = {         host: 'localhost',         port: 7474,         path: '/db/data',         method: 'GET',         headers: {             accept: 'application/json'         }     };  console.log("Start"); var x = http.request(options,function(res){     console.log("Connected");     res.on('data',function(data){         console.log(data);     }); }); 

I check out that the database is running (I connect to the administration web page and everything is working). I am afraid that the problem is not on the database side but on the node.js side.

I hope some could give some light about this issue. I want to learn how to send a http request in node.js, the answer does not have to be specific to the neo4j issue.

Thanks in advance

like image 204
Oni Avatar asked Mar 09 '12 19:03

Oni


People also ask

How do I use HTTP request in node JS?

Setup a new project: To create a new project, enter the following command in your terminal. Project Structure: It will look like the following. Approach 1: In this approach we will send request to getting a resource using AXIOS library. Axios is a promise base HTTP client for NodeJS.

What is HTTP method in node JS?

Node.js has a built-in module called HTTP, which allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP).

How many HTTP requests can node js handle?

js can handle ~15K requests per second, and the vanilla HTTP module can handle 70K rps.


1 Answers

If it's a simple GET request, you should use http.get()

Otherwise, http.request() needs to be closed.

var options = {     host: 'localhost',     port: 7474,     path: '/db/data',     method: 'GET',     headers: {         accept: 'application/json'     } };  console.log("Start"); var x = http.request(options,function(res){     console.log("Connected");     res.on('data',function(data){         console.log(data);     }); });  x.end(); 
like image 155
ming_codes Avatar answered Oct 12 '22 23:10

ming_codes