Skip to content

s-amiour/rest-api-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Rest API Backend Application

This REST API application utilizes the MVC architectural design pattern that separates an app into three parts Models, View, and Controllers. It uses SQLite as database management system, Node.js for JS backend programming, and Express.js for routing and controller abstraction.

Features

  • CRUD operations for multiple resources: Users, Books, Movies, Songs, Video Games
  • Organized structure: Models, Services, Controllers, Routes
  • SQLite database using better-sqlite3
  • Environment configuration with dotenv
  • Simple logging middleware
  • Fully modular and ready for extension

Tech Stack

  • Node.js (v18+ recommended)
  • Express.js (v5)
  • SQLite (via better-sqlite3)
  • Nodemon for development
  • dotenv for environment variables

Installation

  1. Clone the repository
  git clone https://github.com/your-username/rest-api-app.git
  cd rest-api-app
  1. Install dependencies
  npm install
  1. Set up environmental variables Create a .env file in the root directory
# Server port
PORT=3000

# Generate JWT token for authentification
# node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"
JWT_SECRET='default-secret-change-in-production'

# Database URL
# Example for SQLite:
# DATABASE_URL="file:./data/db.sqlite"
# Example for PostgreSQL:
# DATABASE_URL="postgresql://user:password@localhost:5432/mydb"
DATABASE_URL='./db.sqlite'

# Node environment (development, production, test)
NODE_ENV='development'

Don't forget to define your API key. The key name should be x-api-key or authorization.

Project Structure

API/
├── src/
│   ├── config/
│   │   ├── config.js          ← Centralized config
│   │   └── db.js              ← Updated to use config
│   ├── controllers/
│   │   └── bookController.js
│   │   └── movieController.js
│   │   └── songController.js
│   │   └── userController.js
│   │   └── videoGameController.js
│   ├── middleware/
│   │   ├── apiKey.js          ← API key validation
│   │   └── log.js
│   ├── models/
│   │   └── Book.js
│   │   └── Movie.js
│   │   └── Song.js
│   │   └── User.js
│   │   └── VideoGame.js
│   ├── routes/
│   │   └── bookRoutes.js
│   │   └── movieRoutes.js
│   │   └── songRoutes.js
│   │   └── userRoutes.js
│   │   └── videoGameRoutes.js
│   ├── services/
│   │   └── bookService.js
│   │   └── movieService.js
│   │   └── songService.js
│   │   └── userService.js
│   │   └── videoGameService.js
│   └── index.js               ← Updated to use config
│
├── .env                       ← To be created by installer after clone
├── .env.example               ← .env template for installers
├── .gitignore                 ← Includes .env, node_modules/
├── db.sqlite
├── package.json
└── README.md

Architecture Flow

┌─────────────────────────────────────────────────┐
│                   REQUEST                       │
│            GET, POST, PUT, DELETE               │
└─────────────────┬───────────────────────────────┘
                  │
                  ▼
┌─────────────────────────────────────────────────┐
│                  ROUTES                         │
│            (routes/)                            │
│  - Maps endpoints to controller functions       │
└─────────────────┬───────────────────────────────┘
                  │
                  ▼
┌─────────────────────────────────────────────────┐
│               CONTROLLERS                       │
│          (controllers/)                         │
│  - Handles HTTP (req, res, status codes)        │
│  - Calls service layer                          │
└─────────────────┬───────────────────────────────┘
                  │
                  ▼
┌─────────────────────────────────────────────────┐
│                SERVICES                         │
│           (services/)                           │
│  - Business logic & validation                  │
│  - Calls model layer                            │
└─────────────────┬───────────────────────────────┘
                  │
                  ▼
┌─────────────────────────────────────────────────┐
│                 MODELS                          │
│             (models/)                           │
│  - Database schema & operations                 │
│  - SQL queries                                  │
└─────────────────┬───────────────────────────────┘
                  │
                  ▼
┌─────────────────────────────────────────────────┐
│               DATABASE                          │
│            (db.sqlite)                    │
│  - Actual data storage                          │
└─────────────────────────────────────────────────┘

Acknowledgements

Prof. Martin Pedraza

License

ISC

About

⚙️ CRUD REST API backend for EPITA L2/S3 Course: Advanced Web Development

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published