“在调用服务器时出现 [AxiosError: 网络错误]”

huangapple go评论85阅读模式
英文:

getting [AxiosError: Network Error] when calling the server

问题

以下是您提供的代码的中文翻译部分:

在客户端和服务器之间使用axios建立连接。服务器端的代码如下:

在其index.js文件中,我是这样调用路由的:

  1. app.use("/api/v3", routes);

而routes文件如下:

  1. const express = require("express");
  2. const { Router } = express;
  3. const router = Router();
  4. const userRoutes = require("./User");
  5. router.use("/users", userRoutes);
  6. module.exports = router;

而我的userRoutes如下:

  1. router.post("/signup", async (req, res) => {
  2. try {
  3. const { username, email, password, firstName, lastName } = req.body;
  4. const { user, token } = await createUser(
  5. username,
  6. email,
  7. password,
  8. firstName,
  9. lastName
  10. );
  11. res
  12. .status(200)
  13. .json({ message: "用户注册成功", user, token });
  14. } catch (err) {
  15. res.status(500).json({ error: err.message });
  16. }
  17. });

而createUser函数处理将用户保存到数据库的功能。

在我的Postman中,这个API能够正常工作,当我这样调用API时:

  1. http://localhost:3000/api/v3/users/signup

现在,对于客户端的代码,我创建了一个api.js文件如下:

  1. import axios from 'axios';
  2. const api = axios.create({
  3. baseURL: 'http://localhost:3000/api/v3/',
  4. });
  5. // api.defaults.headers.common['Authorization'] = 'Bearer your-token';
  6. export default api;

并创建了一个如下的函数来调用该API:

  1. const createUser = async (username, email, password, firstName, lastName) => {
  2. try {
  3. const response = api.post('users/signup', {
  4. username,
  5. email,
  6. password,
  7. firstName,
  8. lastName,
  9. });
  10. console.log('ff', (await response).data);
  11. const { token, user } = response.data;
  12. auth.setToken(token);
  13. return user;
  14. } catch (error) {
  15. console.log('kk', error.message);
  16. throw error;
  17. }
  18. };

但是在调用这个函数时,我在终端中得到以下结果:

  1. kk Network Error
  2. [AxiosError: Network Error]
  3. 我不确定哪里出了问题。

请注意,上述翻译只包括您提供的代码的相关部分,不包括任何其他内容。如果您需要进一步的帮助或解决问题,请提出具体的问题。

英文:

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:

  1. app.use("/api/v3", routes);

where routes file is like this:

  1. const express = require("express");
  2. const { Router } = express;
  3. const router = Router();
  4. const userRoutes = require("./User");
  5. router.use("/users", userRoutes);
  6. module.exports = router;

and my userRoutes is like this:

  1. router.post("/signup", async (req, res) => { try {
  2. const { username, email, password, firstName, lastName } = req.body;
  3. const { user, token } = await createUser(
  4. username,
  5. email,
  6. password,
  7. firstName,
  8. lastName
  9. );
  10. res
  11. .status(200)
  12. .json({ message: "User registered successfully", user, token });
  13. } catch (err) {
  14. res.status(500).json({ error: err.message });
  15. }
  16. });

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:

  1. http://localhost:3000/api/v3/users/signup

Now, for the client side code, i have created api.js file like this:

  1. import axios from 'axios';
  2. const api = axios.create({
  3. baseURL: 'http://localhost:3000/api/v3/',
  4. });
  5. // api.defaults.headers.common['Authorization'] = 'Bearer your-token';
  6. export default api;

and creeating a function like below to call that api

  1. const createUser = async (username, email, password, firstName, lastName) => {
  2. try {
  3. const response = api.post('users/signup', {
  4. username,
  5. email,
  6. password,
  7. firstName,
  8. lastName,
  9. });
  10. console.log('ff', (await response).data);
  11. const {token, user} = response.data;
  12. auth.setToken(token);
  13. return user;
  14. } catch (error) {
  15. console.log('kk', error.message);
  16. throw error;
  17. }
  18. };

bt on calling this function, i am getting the following results in terminal

  1. kk Network Error
  2. [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

huangapple
  • 本文由 发表于 2023年5月22日 20:36:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定