Small Node.js + Express + mysql2 project: submit a CV through the browser, save rows in MySQL, then run aggregate SQL from a separate script.
MVC (Model–View–Controller) splits responsibilities:
| Piece | In this project | Role |
|---|---|---|
| Model | Not a separate ORM; db.js + SQL |
Data: tables, connections, queries |
| View | views/*.html |
What the user sees (static HTML) |
| Controller | controllers/cvController.js |
Glue: read HTTP input, call DB, choose which view to send |
| Routes | routes/cvRoutes.js |
Maps URLs (GET /, POST /submit) to controller functions |
| Entry | server.js |
Starts the app, connects the DB, mounts routes |
The server file stays thin: it configures Express and starts listening; it does not contain INSERT logic.
database-lab/ # project root (rename your folder if you like)
├── server.js # Entry: Express app + listen + connectDb()
├── db.js # MySQL: create DB, pool, CREATE TABLE
├── controllers/
│ └── cvController.js # showForm, submitForm
├── routes/
│ └── cvRoutes.js # GET / , POST /submit
├── views/
│ ├── index.html # CV form
│ ├── saved.html # After successful save (static message)
│ └── error.html # On error (static message)
├── queries.js # Aggregate SELECTs (run in terminal)
├── package.json
└── README.md
- Node.js 18+ recommended
- MySQL 8 running locally
- A MySQL user that can create databases (e.g.
root) — update password indb.jsto match your machine
-
Install dependencies
cd database-lab npm install -
Configure MySQL credentials
Opendb.jsand setuser/password(two places: the firstcreateConnectionand thecreatePool). -
No manual
CREATE DATABASErequired
The first successfulconnectDb()runsCREATE DATABASE IF NOT EXISTS mycvprojectand creates tables.
npm startOpen http://localhost:3000, fill the form, submit.
- Success →
views/saved.html - Failure →
views/error.html(details are in the terminal viaconsole.error)
node queries.jsor
npm run queriesnpm init→package.jsondb.js— connect, create database, pool, tablesviews/index.html— form withmethod="post"andaction="/submit"server.js— Express,urlencoded,POST /submitwith INSERTs (monolith first)- Split
routes/cvRoutes.js+controllers/cvController.js - Add
queries.jsfor reporting SQL
| Symptom | Things to check |
|---|---|
ECONNREFUSED |
MySQL service not running |
Access denied |
Wrong user / password in db.js |
| Duplicate email error | Email is UNIQUE on person; use another email or clear the table in Workbench |