为什么我在终端中得到了 ECONNRESET,而在浏览器中却没有?
Why do I get ECONNRESET in the terminal, but not the browser?
我有一个非常简单的 nodejs 程序,我用它来演示与 Alchemer 的通信 API。每当我 运行 以下代码时,都会有几秒钟的延迟,然后会抛出标识符 ECONNRESET
的错误。但是,当我将相同的 url 复制到浏览器或 Postman 中时,它工作正常。
Nodejs 代码段
require('dotenv').config();
const https = require('https');
const querystring = require('querystring');
const params = {
api_token: process.env.API_TOKEN,
api_token_secret: process.env.API_TOKEN_SECRET
};
const reqArgs = querystring.stringify(params);
console.log('Query string: ', reqArgs);
const reqOptions = {
host: 'api.alchemer.com',
path: '/v5/survey?' + reqArgs
}
console.log('Query: ', reqOptions.host + reqOptions.path);
console.log('Getting data...');
const req = https.request(reqOptions, resp => {
let data = '';
resp.on('data', chunk => {
data += chunk;
});
resp.on('error', err => {
console.error('Error on response:');
console.error(err);
});
resp.on('end', () => {
console.log('Request complete:');
console.log(data);
});
});
req.on('error', (err) => {
console.error('Error on request:');
console.error(err);
})
观察到错误
Error: socket hang up
at connResetException (internal/errors.js:607:14)
at TLSSocket.socketOnEnd (_http_client.js:493:23)
at TLSSocket.emit (events.js:327:22)
at endReadableNT (internal/streams/readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'ECONNRESET'
}
因为你不打电话 req.end()
!所以它永远不会向后端发送请求。
我有一个非常简单的 nodejs 程序,我用它来演示与 Alchemer 的通信 API。每当我 运行 以下代码时,都会有几秒钟的延迟,然后会抛出标识符 ECONNRESET
的错误。但是,当我将相同的 url 复制到浏览器或 Postman 中时,它工作正常。
Nodejs 代码段
require('dotenv').config();
const https = require('https');
const querystring = require('querystring');
const params = {
api_token: process.env.API_TOKEN,
api_token_secret: process.env.API_TOKEN_SECRET
};
const reqArgs = querystring.stringify(params);
console.log('Query string: ', reqArgs);
const reqOptions = {
host: 'api.alchemer.com',
path: '/v5/survey?' + reqArgs
}
console.log('Query: ', reqOptions.host + reqOptions.path);
console.log('Getting data...');
const req = https.request(reqOptions, resp => {
let data = '';
resp.on('data', chunk => {
data += chunk;
});
resp.on('error', err => {
console.error('Error on response:');
console.error(err);
});
resp.on('end', () => {
console.log('Request complete:');
console.log(data);
});
});
req.on('error', (err) => {
console.error('Error on request:');
console.error(err);
})
观察到错误
Error: socket hang up
at connResetException (internal/errors.js:607:14)
at TLSSocket.socketOnEnd (_http_client.js:493:23)
at TLSSocket.emit (events.js:327:22)
at endReadableNT (internal/streams/readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'ECONNRESET'
}
因为你不打电话 req.end()
!所以它永远不会向后端发送请求。