Skip to content

chanliii/cmpe131-lab5

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Of course. Here is the provided information converted into a comprehensive README.md file.


Node.js & Express API Boilerplate

This project is a comprehensive, best-practice boilerplate for creating scalable and maintainable Node.js applications with Express. The structure is designed to be logical and easy to navigate, promoting a clear separation of concerns by isolating the API layer, business logic, and data access layer.

The core application logic lives inside the src directory to keep it separate from configuration files, tests, and other project metadata.

✨ Features

  • Scalable Folder Structure: A professional structure that separates API (routes, controllers), services (business logic), and repositories (data access).
  • Separation of Concerns: Clear distinction between the Express app configuration (app.js) and the server startup logic (server.js), which is crucial for testing.
  • Centralized Configuration: Uses dotenv to manage environment variables for different environments (development, production).
  • Asynchronous Database Setup: Modern, promise-based SQLite setup using async/await to prevent race conditions.
  • Data Access Layer: Isolates all database queries in a repositories folder, making it easy to switch databases in the future.
  • Database Seeding: Includes a seeding script with @faker-js/faker to populate the database with realistic test data.

📂 Folder Structure

/your-project-name
|
|-- 📂 node_modules/         # Dependencies managed by npm
|-- 📂 src/                  # The heart of your application source code
|   |-- 📂 api/              # API layer: routes, controllers, and middleware
|   |   |-- 📂 routes/       # Defines the API endpoints (e.g., /users, /products)
|   |   |   `-- users.routes.js
|   |   |-- 📂 controllers/  # Handles the request/response logic for each route
|   |   |   `-- user.controller.js
|   |   `-- 📂 middlewares/  # Reusable middleware functions
|   |
|   |-- 📂 config/           # All application configuration
|   |   |-- database.js      # Database connection setup (SQLite)
|   |   `-- index.js         # Loads and exports all config (especially from .env)
|   |
|   |-- 📂 database/         # Database-related files like seeds and migrations
|   |   `-- 📂 seeds/        # Seed files to populate the DB with initial data
|   |       `-- seed.js
|   |
|   |-- 📂 services/         # Business Logic Layer (The "brains" of the app)
|   |   `-- user.service.js
|   |
|   |-- 📂 repositories/     # Data Access Layer (interacts directly with the DB)
|   |   `-- user.repository.js
|   |
|   |-- 📂 utils/            # Utility/helper functions
|   |
|   |-- app.js               # Express app configuration and setup
|   `-- server.js            # The main entry point that starts the server
|
|-- .env                     # Environment variables (NEVER commit to git)
|-- .env.example             # Example environment file
|-- .gitignore               # Files and folders for Git to ignore
|-- package.json             # Project metadata and dependencies
`-- README.md                # Project documentation

🚀 How a Request Flows

Here’s how a POST request to /api/user travels through the application:

  1. server.js: Starts the server, which listens for incoming requests.
  2. app.js: Receives the request. It has been configured to route any path starting with /api to the appropriate router.
  3. api/routes/user.routes.js: Matches the POST /user endpoint and calls the userController.createUser function.
  4. api/controllers/user.controller.js: The createUser function extracts data from req.body and calls userService.createUser(userData). The controller's job is only to manage the request and response, not to perform business logic.
  5. services/user.service.js: The createUser function contains the business logic. It validates the data, checks if the user already exists, and finally calls userRepository.create(newUser).
  6. repositories/user.repository.js: The create function executes the INSERT INTO users (...) SQL query against the database.
  7. Response: The result bubbles back up the chain, and the controller sends a 201 Created status and the new user data back to the client.

🏁 Getting Started

Prerequisites

Installation & Setup

  1. Clone the repository:

    git clone <your-repository-url>
    cd your-project-name
  2. Install dependencies:

    npm install
  3. Set up environment variables: Create a .env file in the root of the project by copying the example file.

    cp .env.example .env

    Open the .env file and set your desired PORT.

    # .env
    PORT=3000
    

    Important: The .env file is already listed in .gitignore to prevent you from accidentally committing sensitive information.

  4. Seed the database: Run the seed script to populate your SQLite database with 20 fake users.

    npm run seed
  5. Start the server:

    npm run start

    The server will start, and you should see the message: Server running on port 3000.


🛠️ Available Scripts

In the package.json file, the following scripts are available:

  • "start": Starts the server using nodemon, which automatically restarts the application when file changes are detected.
  • "seed": Runs the seed.js script to populate the database with initial data.

Endpoints API

All endpoints are prefixed with /api.

Method Endpoint Description
GET /users Retrieve all users.
GET /user/:id Retrieve a single user by ID.
POST /user Create a new user.
PATCH /user/:id Update an existing user.
DELETE /user/:id Delete a user by ID.

About

Sample code for RESTful Service

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%