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.

Rohìt Jíndal
  • 16,572
  • 12
  • 64
  • 110
  • see: [Google](https://www.google.com/search?q=node.js+error%3AReferenceError+), or [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError), – Luuk Mar 04 '22 at 10:41

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();
});