搜索

首页  >  问答  >  正文

无法向后端发出 api 请求,出现 Axios 网络错误

I was making one app with frontend react.js uses axios to api call and for backend i am using express and node with database mysql while making call to api url i am getting error for access denied error

    Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: NO)

If this is not the case i will be getting axios network error

    Axios Network error

 i dont know which port to use if i use 3306 port it is already in use. Please take a look at code and suggest about optimization and a better way...


thank you....

config.js 将保存连接数据库的配置 配置.js

const config = {
      db: {
        host: "localhost",
        user: "new_user",
        password: "password",
        database: "db",

      },
    };

    module.exports = config;

db.js will create the connection
db.js

const mysql = require("mysql");
    const config = require("./config");

    var con = mysql.createPool(config);

    module.exports = con;

这是快递和应用程序服务器应该创建的地方 索引.js

    const express = require("express");
    var mysql = require("mysql");
    const cors = require("cors");
    const app = express();
    app.use(express());
    app.use(cors());
    const Tables路线 = require("./路线/Tables路线");
    //路线
    app.use("/", Tables路线);



    app.listen(8000, () => {
      console.log("Running on port :8000");
    });
从前端调用的 api 路由 路线
    const express = require("express");
    const {
      postTableDes,
      alterTable,
      showTables,
    } = require("../控制器s/Tables");
    const tablerouter = express.Router();

    tablerouter.get("/", showTables);
    tablerouter.post("/", postTableDes);

    module.exports= tablerouter;
在哪里显示表行为 控制器
    const con = require("../Config/db");
    //get tables
    const showTables = async (req, res) => {
      //try {
        var sql = `show tables`;
        const data = await con.query(sql, (err, res)=>{
          if(err){
            console.log(err);
            res.status(404).json({ error: "No show table" });
            return;
          }
          console.log(res);
          res.status(202).json(data);
        });
     module.exports = showTables;

P粉288069045P粉288069045240 天前402

全部回复(1)我来回复

  • P粉916553895

    P粉9165538952024-03-30 09:22:15

    尝试将 config.js 更改为(createPool 调用不需要具有 db 属性的对象):

    const config = {
       host: "localhost",
       user: "new_user",
       password: "password",
       database: "db",
    };
    
    module.exports = config;

    回复
    0
  • 取消回复