Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node.js Learning Starter

This project is designed for someone coming from a React Native background and learning backend basics with Node.js.

Project Structure

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

Setup

  1. Install dependencies:

    npm install
  2. Create your env file:

    cp .env.example .env
  3. 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;"
  4. Create tables with raw SQL:

    mysql -u root -p learning_node < src/db/schema.sql
  5. Start in dev mode:

    npm run dev
  6. Test endpoint:

    • GET http://localhost:5001/api/health

CRUD Endpoints (Users)

  • GET http://localhost:5001/api/users
  • GET http://localhost:5001/api/users/:id
  • POST http://localhost:5001/api/users
  • PATCH http://localhost:5001/api/users/:id
  • DELETE http://localhost:5001/api/users/:id

Example create payload:

{
  "name": "Alex",
  "email": "alex@example.com"
}

Example fetch users:

curl http://localhost:5001/api/users

Common DB Error

If you get ECONNREFUSED, your app cannot reach MySQL.

Quick fix:

  1. Start DB: npm run db:up
  2. Ensure DB exists: mysql -u root -e "CREATE DATABASE IF NOT EXISTS learning_node;"
  3. Ensure .env has: DATABASE_URL=mysql://root:password@localhost:3306/learning_node
  4. Create tables again: mysql -u root -p learning_node < src/db/schema.sql

Why this structure?

  • 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.

About

Learning node js

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages