关于axios的Error: connect ECONNREFUSED 127.0.0.1:7001
来源:5-2 脚手架请求项目模板API开发
慕斯4168185
2021-04-03 07:09:12
问题描述:
问题发生在阶段一的第五周,5-2 脚手架请求项目模板API开发 这一节
我跟着视频写的代码但还是会显示 Error: connect ECONNREFUSED 127.0.0.1:7001
我使用的操作系统是win10
麻烦老师解答一下。
相关截图:
我在postman上可以通过api获取到数据
imooc-cli-dev里的错误信息:
Error: connect ECONNREFUSED 127.0.0.1:7001
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 7001,
config: {
url: '/project/template',
headers: {
Accept: 'application/json, text/plain, */*',
'User-Agent': 'axios/0.21.1'
},
baseURL: 'http://localhost:7001',
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 7000,
adapter: [Function: httpAdapter],
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus],
method: 'get',
data: undefined
},
request: <ref *1> 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,
afterWriteTickInfo: null,
buffered: [],
bufferedIndex: 0,
allBuffers: true,
allNoop: true,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
errored: null,
closed: false
},
_events: [Object: null prototype] {
response: [Array],
error: [Function: handleRequestError]
},
_eventsCount: 2,
_maxListeners: undefined,
_options: {
maxRedirects: 21,
maxBodyLength: 10485760,
protocol: 'http:',
path: '/project/template',
method: 'GET',
headers: [Object],
agent: undefined,
agents: [Object],
auth: undefined,
hostname: 'localhost',
port: '7001',
nativeProtocols: [Object],
pathname: '/project/template'
},
_ended: true,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 0,
_requestBodyBuffers: [],
_onNativeResponse: [Function (anonymous)],
_currentRequest: ClientRequest {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Socket],
_header: 'GET /project/template HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'User-Agent: axios/0.21.1\r\n' +
'Host: localhost:7001\r\n' +
'Connection: close\r\n' +
'\r\n',
_keepAliveTimeout: 0,
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
method: 'GET',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
path: '/project/template',
_ended: false,
res: null,
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'localhost',
protocol: 'http:',
_redirectable: [Circular *1],
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype]
},
_currentUrl: 'http://localhost:7001/project/template',
_timeout: Timeout {
_idleTimeout: -1,
_idlePrev: null,
_idleNext: null,
_idleStart: 71610,
_onTimeout: null,
_timerArgs: undefined,
_repeat: null,
_destroyed: true,
[Symbol(refed)]: true,
[Symbol(kHasPrimitive)]: false,
[Symbol(asyncId)]: 14,
[Symbol(triggerId)]: 11
},
[Symbol(kCapture)]: false
},
response: undefined,
isAxiosError: true,
toJSON: [Function: toJSON]
}
项目名imooc verb Error: connect ECONNREFUSED 127.0.0.1:7001
项目名imooc ERR! connect ECONNREFUSED 127.0.0.1:7001
项目名imooc verb 命令执行成功:0
相关代码:
request.js
'use strict';
const axios= require('axios')
const BASE_URL= process.env.IMOOC_CLI_BASE_URL ? process.env.IMOOC_CLI_BASE_URL : 'http://localhost:7001';
console.log('BASE_URL',BASE_URL);
const request = axios.create({
baseURL:BASE_URL,
timeout:7000,
})
request.interceptors.response.use(
response=>{
console.log(response.data);
return response.data;
},
error=>{
console.log(error);
return Promise.reject(error)
}
)
module.exports = request;
相关代码:
commands/init/lib/get_project_template.js
const request=require("@imooc-cli-dev-zhang/request")
module.exports=function(){
return request({
url:'/project/template',
});
}
相关代码:
commands/init/lib/init.js
async prepare(){
// 0 ,判断项目模板是否存在
const template=await get_project_template();
}
尝试过的解决方式:
我尝试用以上代码呼叫外部的api,https://jsonplaceholder.cypress.io/todos/1
是正常运行的
1回答
扬_灵
2021-04-04
同学你好,重启数据库然后在连接试一下, 后端启动后把接口地址现在浏览器中试一下看看能不能使用。
相似问题