node.js 在应用程序中发出 GET 请求时 ECONNREFUSED,但 API 成功返回 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34147169/
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
ECONNREFUSED when making GET request in app, but API returns JSON successfully
提问by johnprisco
I'm writing a node app with React, using node-postgres and superagent for backend calls. Let's say I'm making a GET request and using the JSON it returns to fill a table of students. My API looks like this:
我正在用 React 编写一个节点应用程序,使用 node-postgres 和 superagent 进行后端调用。假设我正在发出 GET 请求并使用它返回的 JSON 来填充学生表。我的 API 看起来像这样:
import pg from 'pg';
import Router from 'express';
let router = new Router();
let conString = "postgres://user:pass@localhost/db_name";
router.get('/getStudents', function(req, res) {
var results = [];
pg.connect(conString, function(err, client, done) {
if (err) {
done();
console.log(err);
return res.status(500).json({success: false, data: err});
}
var query = client.query('SELECT first_name, last_name, email FROM students');
query.on('row', function(row) {
results.push(row);
});
query.on('end', function() {
done();
return res.json(results);
});
});
});
On page load, this is called from the store to set a students array. It seems like something is going wrong here:
在页面加载时,从商店调用它来设置一个学生数组。这里似乎出了点问题:
var request = require('super agent');
function getStudents() {
request
.get('/api/getStudents')
.set('Accept', 'application/json')
.end(function(err, res) {
if (err) {
console.log("There's been an error: getting students.");
console.log(err);
} else {
return res;
}
});
}
If I curl localhost:3000/api/getStudents, I get the JSON response I expect.
如果我 curl localhost:3000/api/getStudents,我会得到我期望的 JSON 响应。
However, when I call this on page load, I get an ECONNREFUSED error:
但是,当我在页面加载时调用它时,出现 ECONNREFUSED 错误:
Error: connect ECONNREFUSED 127.0.0.1:80]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80,
response: undefined
Not sure why I'm getting an error on the HTTP port. This is my first time using node-postgres, superagent, and React so any help is appreciated.
不知道为什么我在 HTTP 端口上收到错误。这是我第一次使用 node-postgres、superagent 和 React,因此感谢您的帮助。
Edit:Forgot to mention that I'm able to make POST requests and insert items into the database without any errors. This error only occurs when I'm attempting a GET request.
编辑:忘记提及我能够发出 POST 请求并将项目插入数据库而不会出现任何错误。此错误仅在我尝试 GET 请求时发生。
采纳答案by Quy
Try this (inserting the full path url) in the getmethod:
在get方法中试试这个(插入完整路径 url):
request
.get('http://localhost:3000/api/getStudents')
.set('Accept', 'application/json')
.end(function(err, res) {
if (err) {
console.log("There's been an error: getting students.");
console.log(err);
} else {
return res;
}
});
Check out the documentation for CORS for an example of using absolute urls:
查看 CORS 的文档以获取使用绝对 url 的示例:
回答by Kim Kern
The error will also occur if you don't have the protocol in your request URL.
如果您的请求 URL 中没有协议,也会发生该错误。
Instead
反而
request.get('www.myexample.com/api/getStudents')
do
做
request.get('https://www.myexample.com/api/getStudents')
^^^^^^^^

