Project setup
Before you connect to the database, make sure you have a basic Express project set up. If you need a reminder from Module 1:
- Create a new project directory.
- Run
npm init -yto create apackage.json. - Install Express,
cors, TypeScript and the relevant type packages. - Add
"type": "module"topackage.jsonfor ES modules. - Add the
devscript inpackage.json.
What is mysql2?
The mysql2 library is a Node.js package that lets your Express server connect to and communicate with MySQL databases. It handles the networking and protocol details so you can focus on writing your API logic.
Installing mysql2
In your Express project directory, install mysql2 and dotenv:
npm install mysql2 dotenv