Ads Top

How to create connection in node js with mysql?

 Connectivity node with react:

 

First, you need to create a node project and then create a server.js file and use the below code. Need to install express, body-parser, cors, MySQL.

Use this command: npm i mysql cors body-parser express

 

srever.js 

const express = require('express')

const http = require('http')

const app = express()

const bodyParser = require('body-parser')

const cors = require('cors')

const mysql = require('mysql')

 

const apiPort = 2020 // You can decide your port name as you wish

 

app.use(bodyParser.urlencoded({ extended: true }))

app.use(cors())

app.use(bodyParser.json())

 

const db = mysql.createConnection({

    user: "yourUserName_user",

    password : "yourPass@!@#`98",

    host: "168.200.000.00",

    database : "yourDbName_db",

    // for the locally conectivity

    // user: "yourusername",

    // password: "yourpassword",

    // host: "localhost"

})

 

db.connect(function(err) {

    if (err) throw err

});

 

// Here will come your new code.

 

app.listen(apiPort, () => console.log(`Server running on port ${apiPort}`))

 

 

For single value you need to add in the server.js file:

 

app.get('/category', (req, res) => {

    db.query('SELECT id,cateName FROM tablecategory ORDER BY cateName ASC', (err, result)=>{

        if(err){

            console.log(err);

        }

        else{

            res.send(result)

        }

        //db.close();

    })

   

})

 

app.post('/addcategory', (req, res) => {

    // db.connect();

    console.log(req.body.ExerciseName);

     db.query('INSERT INTO tablecategory (cateName) VALUES("'+req.body.cateTextName+'")', (err, result)=>{

         if(err){

             console.log(err);

         }

         else{

             res.send(result)

         }

         //db.close();

     })

  })

 

For multiple value you need to add in the server.js file:

 // New test Category

 app.get('/getTestCategory', (req, res) => {

    // db.connect();

     db.query('SELECT id,testName,testclassName,testtext,orgiName FROM tableTestCategory ORDER BY testName ASC', (err, result)=>{

         if(err){

             console.log(err);

         }

         else{

             res.send(result)

         }

         //db.close();

     })

     

 })

 

 app.post('/addTestCategory', (req, res) => {

     // db.connect();

     console.log(req.body.testName);

      db.query('INSERT INTO tableTestCategory(testName,testclassName,testtext, orgiName) VALUES("'+req.body.testName+'", "'+req.body.testclassName+'", "'+req.body.testtextLevel+'", "'+req.body.orgiName+'")', (err, result)=>{

          if(err){

              console.log(err);

          }

          else{

              res.send(result)

          }

          //db.close();

      })

  })

 

Need to go on the react project where you want to call your data which coming from the mySql server. You can fetch your data as using axios:

 

let [exerciseLists, setExerciseLists] = useState([]);

 

        useEffect( () => {

          axios.get('http://localhost:2020/getTestCategory')

          .then(response => setExerciseLists(response.data))

          //.then(response => console.log(response.data))

      }, [])

 

And you can access your data with react components.

<YourComponent property={exerciseLists} />





No comments:

Note: Only a member of this blog may post a comment.

Copyright Reserved to Anything Learn. Powered by Blogger.