无法向后端发出API请求,获取Axios网络错误

k97glaaz  于 2022-11-23  发布在  iOS
关注(0)|答案(1)|浏览(153)
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将为连接数据库config.js保存configq

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;

express和应用服务器应该在这里创建index.js

const express = require("express");
    var mysql = require("mysql");
    const cors = require("cors");
    const app = express();
    app.use(express());
    app.use(cors());
    const TablesRoutes = require("./routes/TablesRoutes");
    //routes
    app.use("/", TablesRoutes);


    app.listen(8000, () => {
      console.log("Running on port :8000");
    });

API路由到来自前端路由的呼叫

const express = require("express");
    const {
      postTableDes,
      alterTable,
      showTables,
    } = require("../Controllers/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;
w41d8nur

w41d8nur1#

尝试将config.js变更为(createPool呼叫不需要具有db属性的对象):

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

module.exports = config;

相关问题