Error: Request failed with status code 404
at createError (D:\NodeJs\node-demo\node_modules\axios\lib\core\createError.js:16:15)
at settle (D:\NodeJs\node-demo\node_modules\axios\lib\core\settle.js:17:12)
at IncomingMessage.handleStreamEnd (D:\NodeJs\node-demo\node_modules\axios\lib\adapters\http.js:293:11)
at IncomingMessage.emit (node:events:402:35)
at endReadableNT (node:internal/streams/readable:1340:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
config: {
transitional: {
silentJSONParsing: true,
forcedJSONParsing: true,
clarifyTimeoutError: false
},
adapter: [Function: httpAdapter],
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 1000,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus],
headers: {
Accept: 'application/json, text/plain, */*',
'User-Agent': 'axios/0.24.0'
},
baseUrl: 'http://poetry.apiopen.top/',
method: 'get',
url: '/sentences',
data: undefined
},
var axios = require('axios');
var axiosInstance = axios.create({
baseUrl: 'http://poetry.apiopen.top/',
timeout: 1000
});
axiosInstance
.get('/sentences')
.then(function (res) {
console.log(res.data)
});
根据提示报出的响应状态码404,知道是路径的问题,但这个请求接口路径没有问题,但是这里使用了axios的baseURL,所以问题出在这。
我们看官网的示例代码是:
const instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
仔细看原来我们是把baseURL
属性写成了baseUrl
,所以导致报错。
把baseUrl
改成baseURL
。
var axios = require('axios');
var axiosInstance = axios.create({
baseURL: 'http://poetry.apiopen.top/',
timeout: 1000
});
axiosInstance
.get('/sentences')
.then(function (res) {
console.log(res.data)
});