我通过Droplet(数字海洋)中的Express服务器内部的(Axios)向自签名SSL主机发出了get请求,并得到未定义的响应和错误:
{ Error: timeout of 10000ms exceeded
at createError (/home/apps/myHealth_backend/node_modules/axios/lib/core/createError.js:16:15)
at Timeout.handleRequestTimeout [as _onTimeout] (/home/apps/myHealth_backend/node_modules/axios/lib/adapters/http.js:216:16)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
config:
{ adapter: [Function: httpAdapter],
transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
timeout: 10000,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
headers:
{ Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ...',
'User-Agent': 'axios/0.18.0' },
method: 'get',
withCredentials: true,
httpsAgent:
Agent {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: [Object] },
url: 'https://example.kz/services/api/Person?fioiin=123121312',
data: undefined },
code: 'ECONNABORTED',
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,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
domain: null,
_events:
{ response: [Function: handleResponse],
error: [Function: handleRequestError] },
_eventsCount: 2,
_maxListeners: undefined,
_options:
{ protocol: 'https:',
maxRedirects: 21,
maxBodyLength: 10485760,
path: '/services/api/Person?fioiin=123121312',
method: 'get',
headers: [Object],
agent: [Object],
auth: undefined,
hostname: 'example.kz',
port: null,
nativeProtocols: [Object],
pathname: '/services/api/Person',
search: '?fioiin=123121312' },
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 0,
_requestBodyBuffers: [],
_onNativeResponse: [Function],
_currentRequest:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Object],
connection: [Object],
_header: 'GET /services/api/Person?fioiin=123121312 HTTP/1.1\r\nAccept: application/json\r\nContent-Type: application/json\r\nAuthorization: Bearer ...\r\nUser-Agent: axios/0.18.0\r\nHost: rpn.eisz.kz\r\nConnection: close\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/services/api/Person?fioiin=123121312',
_ended: false,
res: null,
aborted: 1536482900204,
timeoutCb: null,
upgradeOrConnect: false,
parser: [Object],
maxHeadersCount: null,
_redirectable: [Circular],
[Symbol(outHeadersKey)]: [Object] },
_currentUrl: 'https://example.kz/services/api/Person?fioiin=123121312' },
response: undefined }
如果我在本地运行它,则可以,但是当我将其放置在Server(数字海洋)中时,它将不起作用。 服务器是Ubuntu ubuntu 18.04,节点8.11.4,axios 0.18.0 示例代码:
const express = require('express');
const app = express()
const axios = require('axios')
const process = require('process')
const https = require('https')
const Querystring = require('querystring')
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
app.get('/', (req, res)=>{
console.log('test')
res.send('testing')
})
app.get('/api/iin/:iin', (req, res)=>{
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
console.log('STAGE 1 ', req.params.iin)
const url_iin = 'https://example.kz/services/api/Person?fioiin='+req.params.iin;
const TOKEN = "Bearer ..."
const instance = axios.create({
timeout: 10000,
withCredentials: true,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
});
instance.get(url_iin, {headers: {Authorization: TOKEN}})
.then(response=>{
console.log(response)
res.send('OK')
})
.catch(error=>{
console.log(error)
res.send('SERVER ERROR')
})
})
app.listen(3000, ()=>{
console.log(`listening port 3000...`)
})