在 node.js 中发送 http 请求 [英] Sending http request in node.js

查看:25
本文介绍了在 node.js 中发送 http 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 node.js 向 neo4j 数据库发送 http 请求.这是我正在使用的代码:

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);
    });
});

我检查了数据库是否正在运行(我连接到管理网页并且一切正常).恐怕问题不在数据库端,而是在 node.js 端.

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.

我希望有人能对此问题有所了解.我想学习如何在 node.js 中发送 http 请求,答案不必特定于 neo4j 问题.

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.

提前致谢

推荐答案

如果是简单的GET请求,应该使用http.get()

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

否则需要关闭http.request().

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();

这篇关于在 node.js 中发送 http 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆