在 node.js 中发送 http 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9639978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:18:57  来源:igfitidea点击:

Sending http request in node.js

httpnode.jsrequest

提问by Oni

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

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

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.

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

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.

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

Thanks in advance

提前致谢

回答by ming_codes

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

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

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

否则,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();