22

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

Oni
  • 798
  • 2
  • 8
  • 17
  • [For someone looking for HTTPS](http://stackoverflow.com/questions/13121590/steps-to-send-a-https-request-to-a-rest-service-in-node-js), [HTTPS using default Node HTTPS Module](http://stackoverflow.com/a/13127859/452708) – Abhijeet Apr 27 '17 at 04:14

2 Answers2

37

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();
ming_codes
  • 2,870
  • 25
  • 24
-2

I highly recommend to you use this minimal and easy package to send request on nodejs

Install package

npm install smoothly-request

Code that sends request

const smoothlyRequest = require('smoothly-request');

(async () => {
  const result = await smoothlyRequest({
      hostname: `http://localhost:7474`,
      path: '/db/data',
      method: 'GET'
  });
})();