0

I am using below lines :

var express = require("express");
var mysql2 = require("mysql")
var app = mysql2();
var app = express();
app.listen(3000, function(){
    console.log('App Listening on port 3000');
});

Everytime I try to run: node index.js I receive below error :

ReferenceError: mysql2 is not defined

Please, advise how that can be solved and what is the problem.

Debug Diva
  • 26,058
  • 13
  • 70
  • 123

1 Answers1

0

First, the problem is you can not create two same variables - app. Second, I think what you want to do is to connect the MySQL database, the code should be

const db = mysql.createConnection({ 
    user: "root",
    host: "localhost",
    password: "password",
    database: "your_database_name",
  });

If you encounter Client authentication protocol problems, please refer to this answer.
Third, when you get a request from the client and want to have some modification to the database, you can have code like

app.post("/modify", async (req, res) => {
  await modifyDatabase(req.body); // your modify function
  res.status(200).send();
});