This project is designed for someone coming from a React Native background and learning backend basics with Node.js.
learning-node/
├─ src/
│ ├─ app.js # App setup (middleware + routes)
│ ├─ server.js # Server bootstrap
│ ├─ config/
│ │ └─ env.js # Environment variable config
│ ├─ db/
│ │ ├─ mysql.js # Raw MySQL connection pool
│ │ └─ schema.sql # Raw SQL table setup
│ ├─ routes/
│ │ ├─ index.js # Route aggregator
│ │ └─ health.routes.js # Health check route
│ │ └─ users.routes.js # User CRUD routes
│ ├─ controllers/
│ │ └─ health.controller.js # Controller logic
│ │ └─ users.controller.js # User CRUD controllers
│ ├─ services/
│ │ └─ health.service.js # Business logic layer
│ │ └─ users.service.js # MySQL user CRUD logic
│ ├─ middlewares/
│ │ └─ error.middleware.js # Global error handling
│ └─ utils/
│ └─ api-response.js # Shared response helper
├─ docs/
│ └─ learning-path.md # Suggested topic roadmap
├─ .env.example
├─ .gitignore
└─ package.json
-
Install dependencies:
npm install
-
Create your env file:
cp .env.example .env
-
Create MySQL database:
- Database name:
learning_node - Default connection in
.env.example:mysql://root:password@localhost:3306/learning_node
Install MySQL (one time):
brew install mysql
Start MySQL service:
npm run db:up
Create DB user and database (first time only):
mysql -u root -e "CREATE DATABASE IF NOT EXISTS learning_node;" mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'password'; FLUSH PRIVILEGES;"
- Database name:
-
Create tables with raw SQL:
mysql -u root -p learning_node < src/db/schema.sql -
Start in dev mode:
npm run dev
-
Test endpoint:
GET http://localhost:5001/api/health
GET http://localhost:5001/api/usersGET http://localhost:5001/api/users/:idPOST http://localhost:5001/api/usersPATCH http://localhost:5001/api/users/:idDELETE http://localhost:5001/api/users/:id
Example create payload:
{
"name": "Alex",
"email": "alex@example.com"
}Example fetch users:
curl http://localhost:5001/api/usersIf you get ECONNREFUSED, your app cannot reach MySQL.
Quick fix:
- Start DB:
npm run db:up - Ensure DB exists:
mysql -u root -e "CREATE DATABASE IF NOT EXISTS learning_node;" - Ensure
.envhas:DATABASE_URL=mysql://root:password@localhost:3306/learning_node - Create tables again:
mysql -u root -p learning_node < src/db/schema.sql
- Familiar separation similar to React Native feature layers.
- Clear distinction between routes, controllers, and services.
- Easy to scale from a small learning app to production patterns.