Javascript Can';在没有完整url的情况下,无法让axios执行get
每次我尝试在axios请求中使用/bikes或/bikes/添加时,它似乎从未连接。我总是得到这样的东西:Javascript Can';在没有完整url的情况下,无法让axios执行get,javascript,express,routes,axios,mern,Javascript,Express,Routes,Axios,Mern,每次我尝试在axios请求中使用/bikes或/bikes/添加时,它似乎从未连接。我总是得到这样的东西: // client.js var axios = require('axios'); var axiosInstance = axios.create({ baseURL: 'http://localhost:4000', /* other custom settings */ }); module.exports = axiosInstance; xhr.js:1
// client.js
var axios = require('axios');
var axiosInstance = axios.create({
baseURL: 'http://localhost:4000',
/* other custom settings */
});
module.exports = axiosInstance;
xhr.js:178获取http://localhost:3000/bikes/ 404(未找到)
但是,当我使用完整的url时,比如:http://localhost:4000/bikes/
它连接得很好。我试图在server.js中弄乱app.get,在我的路线文件中弄乱get,在我的自行车列表文件中弄乱实际的axios.get,但都没有用
有人有什么想法吗?这是MERN应用程序的一部分
bikes list.js(组件)代码段:
server.js代码段:
app.use('/bikes', bikeRoutes);
js(路线)代码段:
谢谢 原因可能是您在使用
/bikes
时没有使用正确的端口?一种解决方案是创建如下小模块:
// client.js
var axios = require('axios');
var axiosInstance = axios.create({
baseURL: 'http://localhost:4000',
/* other custom settings */
});
module.exports = axiosInstance;
然后在代码中使用这个新模块,而不需要axios
直接:
var client = require('./client');
client.get('relative/path')
3000美元卖什么?它有到4000的代理吗?除非您告诉它,否则它怎么知道您想向另一台服务器(在端口4000上运行)而不是HTML文档来自的服务器(在端口3000上)发出请求?
var client = require('./client');
client.get('relative/path')