gpt4 book ai didi

javascript - 从 Node API 到 PHP 服务器的 Axios 调用导致套接字挂起

转载 作者:太空宇宙 更新时间:2023-11-04 01:26:24 30 4
gpt4 key购买 nike

我使用 Express 在 Nodejs 中创建了一个 API。

在调用 Node API 时,它会读取一个文件并将其发送到部署在 Apache 中的 PHP 服务器。从 PHP 服务器接收响应并将其发送回 Node API 的调用者。第一次点击我的 Node API 时,它会返回正确的结果。从第二次点击开始“Socket Hang up” 就是我收到的错误。

var express = require('express')
var app = express()
const axios = require('axios');
var path = require('path');
const FormData = require('form-data');
const form = new FormData();
var fs = require('fs');
var dir = './tmp';

app.get('/file', function(request, responses) {

new Promise((resolve , reject)=>{
form.append('file',fs.createReadStream(dir+'/FileCreated.txt'));
resolve(form);
}).then(form => {
console.log('Sending file to the PHP ');
let url = 'http://121.115.158.12/upload.php';
axios({
method: 'post',
url: url,
// timeout : 3000,
data: form,
headers: {
'content-type': `multipart/form-data;boundary=${form._boundary} `,//
}
})
.then(function (response) {
console.log(response.status);

responses.send({status:response.status , data: response.data});
responses.end();
})
.catch(function (error) {
console.log(error);
responses.send({status:error.status , data: error.Error});
responses.end();
});

request.on('end', function() {
console.log('close');
});

})

})

我从第二个 Axios 开始收到的错误。

{ Error: socket hang up
at createHangUpError (_http_client.js:323:15)
at Socket.socketOnEnd (_http_client.js:426:23)
at Socket.emit (events.js:194:15)
at endReadableNT (_stream_readable.js:1103:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
code: 'ECONNRESET',
config:
{ url: 'http://121.115.158.12/upload.php/',
method: 'post',
data:
FormData {
_overheadLength: 314,
_valueLength: 0,
_valuesToMeasure: [Array],
writable: false,
readable: true,
dataSize: 0,
maxDataSize: 2097152,
pauseStreams: true,
_released: true,
_streams: [Array],
_currentStream: null,
_insideLoop: false,
_pendingNext: false,
_boundary: '--------------------------691559357280045881646354',
_events: [Object],
_eventsCount: 1 },
headers:
{ Accept: 'application/json, text/plain, */*',
'Content-Type':
'multipart/form-data;boundary=--------------------------691559357280045881646354 ',
'User-Agent': 'axios/0.19.0' },
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
adapter: [Function: httpAdapter],
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus] },
request:
Writable {
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: true,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
_events:
[Object: null prototype] {
response: [Function: handleResponse],
error: [Function: handleRequestError] },
_eventsCount: 2,
_maxListeners: undefined,
_options:
{ protocol: 'http:',
maxRedirects: 21,
maxBodyLength: 10485760,
path: '/upload.php/',
method: 'POST',
headers: [Object],
agent: undefined,
auth: undefined,
hostname: '121.115.158.12',
port: null,
nativeProtocols: [Object],
pathname: '/upload.php/' },
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 0,
_requestBodyBuffers: [],
_onNativeResponse: [Function],
_currentRequest:
ClientRequest {
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: false,
_headerSent: false,
socket: [Socket],
connection: [Socket],
_header: null,
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/upload.php/',
_ended: false,
res: null,
aborted: undefined,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
_redirectable: [Circular],
[Symbol(isCorked)]: false,
[Symbol(outHeadersKey)]: [Object] },
_currentUrl: 'http://121.115.158.12/upload.php/' },
response: undefined,
isAxiosError: true,
toJSON: [Function] }

观察结果:

1)对 PHP 服务器(部署在 Apache 中)的第一次命中正确记录在 Apache 的日志文件中,但从第二次命中开始,Apache 中没有日志,可能会告诉我 Axios 请求甚至没有到达 Apache?

2)我又构建了一个 Node API,只是为了检查我是否点击了该 API,然后我会收到套接字挂起错误,但该 API 每次都会运行。即使我在上述 API 上遇到套接字挂起,这个 API 也能完美运行。

app.get('/random', function(request, responses) {
console.log(responses);
responses.send('Just to check');
})

最佳答案

仍然无法找到问题的原因。我找到了一个回旋处,并使用 shell 脚本通过 Shell.js 访问 PHP 服务器。

这个解决方案效果很好。

关于javascript - 从 Node API 到 PHP 服务器的 Axios 调用导致套接字挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57379364/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com