英文:
getting [AxiosError: Network Error] when calling the server
问题
以下是您提供的代码的中文翻译部分:
在客户端和服务器之间使用axios建立连接。服务器端的代码如下:
在其index.js文件中,我是这样调用路由的:
app.use("/api/v3", routes);
而routes文件如下:
const express = require("express");
const { Router } = express;
const router = Router();
const userRoutes = require("./User");
router.use("/users", userRoutes);
module.exports = router;
而我的userRoutes如下:
router.post("/signup", async (req, res) => {
try {
const { username, email, password, firstName, lastName } = req.body;
const { user, token } = await createUser(
username,
email,
password,
firstName,
lastName
);
res
.status(200)
.json({ message: "用户注册成功", user, token });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
而createUser函数处理将用户保存到数据库的功能。
在我的Postman中,这个API能够正常工作,当我这样调用API时:
http://localhost:3000/api/v3/users/signup
现在,对于客户端的代码,我创建了一个api.js文件如下:
import axios from 'axios';
const api = axios.create({
baseURL: 'http://localhost:3000/api/v3/',
});
// api.defaults.headers.common['Authorization'] = 'Bearer your-token';
export default api;
并创建了一个如下的函数来调用该API:
const createUser = async (username, email, password, firstName, lastName) => {
try {
const response = api.post('users/signup', {
username,
email,
password,
firstName,
lastName,
});
console.log('ff', (await response).data);
const { token, user } = response.data;
auth.setToken(token);
return user;
} catch (error) {
console.log('kk', error.message);
throw error;
}
};
但是在调用这个函数时,我在终端中得到以下结果:
kk Network Error
[AxiosError: Network Error]
我不确定哪里出了问题。
请注意,上述翻译只包括您提供的代码的相关部分,不包括任何其他内容。如果您需要进一步的帮助或解决问题,请提出具体的问题。
英文:
I am trying to establish a connection using axios between client and server, the server side code is like this:
in its index.js file, i am calling routes like this:
app.use("/api/v3", routes);
where routes file is like this:
const express = require("express");
const { Router } = express;
const router = Router();
const userRoutes = require("./User");
router.use("/users", userRoutes);
module.exports = router;
and my userRoutes is like this:
router.post("/signup", async (req, res) => { try {
const { username, email, password, firstName, lastName } = req.body;
const { user, token } = await createUser(
username,
email,
password,
firstName,
lastName
);
res
.status(200)
.json({ message: "User registered successfully", user, token });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
and the createUser function is handling the saving functionality of the user into the database.
this api is working correctly in my postman, when i call the api like this:
http://localhost:3000/api/v3/users/signup
Now, for the client side code, i have created api.js file like this:
import axios from 'axios';
const api = axios.create({
baseURL: 'http://localhost:3000/api/v3/',
});
// api.defaults.headers.common['Authorization'] = 'Bearer your-token';
export default api;
and creeating a function like below to call that api
const createUser = async (username, email, password, firstName, lastName) => {
try {
const response = api.post('users/signup', {
username,
email,
password,
firstName,
lastName,
});
console.log('ff', (await response).data);
const {token, user} = response.data;
auth.setToken(token);
return user;
} catch (error) {
console.log('kk', error.message);
throw error;
}
};
bt on calling this function, i am getting the following results in terminal
kk Network Error
[AxiosError: Network Error]
i am not sure what is wrong here
答案1
得分: 0
我刚刚不得不将基本URL从本地主机更改为具有我的IP地址值,并且它有效。
英文:
i just had to change the baseURL from localhost to have the value of my ip address and it worked
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论