Skip to content

tecnodeveloper/nodejs-base-project

Repository files navigation

Express-Nodejs template project (Node.js + Express + Prisma + SQLite)

Backend API for a Book Store system built with Node.js and Express.
Includes Prisma ORM with SQLite, Swagger docs, linting, formatting, and Docker support.


Features

  • Express server with basic folder structure
  • Health check endpoint (/health)
  • SQLite database (file-based)
  • Prisma ORM integration
  • Swagger API documentation (/api-docs)
  • ESLint + Prettier setup
  • Pre-commit hooks with Husky + lint-staged
  • Nodemon for auto-reload
  • Docker support

πŸ›  Tech Stack

  • Node.js
  • Express
  • Prisma ORM
  • SQLite
  • Swagger (swagger-ui-express, swagger-jsdoc)
  • ESLint + Prettier
  • Husky + lint-staged
  • Docker

Project Structure


.
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ routes/
β”‚   β”œβ”€β”€ controllers/
β”‚   β”œβ”€β”€ services/
β”‚   β”œβ”€β”€ middlewares/
β”‚   └── app.js
β”œβ”€β”€ prisma/
β”‚   └── schema.prisma
β”œβ”€β”€ .husky/
β”œβ”€β”€ .eslintrc
β”œβ”€β”€ .prettierrc
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ package.json
└── README.md


Setup & Installation

npm install

Run Development Server

npm run dev
  • Server runs on: http://localhost:3000
  • Health check: GET /health
  • Swagger docs: http://localhost:3000/api-docs

Database Setup (Prisma + SQLite)

Initialize Prisma

npx prisma init

Run Migration

npm run prisma:migrate

Generate Prisma Client

npm run prisma:generate

Prisma Model

model Book {
  id        Int     @id @default(autoincrement())
  name      String
  author    String
  price     Float
  publisher String?
}

Linting & Formatting

Check lint

npm run lint

Fix lint issues

npm run lint:fix

Format code

npm run format

Pre-commit Hooks

  • ESLint runs on staged files
  • Prettier formats staged files
  • Commit blocked on errors

Docker

Build Image

docker build -t bookstore-api .

Run Container

docker run -p 3000:3000 bookstore-api

Scripts

"scripts": {
  "start": "node src/server.js",
  "dev": "nodemon src/server.js",
  "lint": "eslint .",
  "lint:fix": "eslint . --fix",
  "format": "prettier --write .",
  "prisma:migrate": "npx prisma migrate dev",
  "prisma:generate": "npx prisma generate"
}

Environment Variables

Create a .env file:

DATABASE_URL="file:./dev.db"
PORT=3000

Status

  • Core setup complete
  • Database connected
  • Tooling configured
  • Dockerized

Books API

POST /books ""Endpoint**

Description: Creates a new book with multiple authors and stores it in the database.


Request Body

{
  "name": "Atomic Habits",
  "authors": ["Zain", "Chris"],
  "price": 100,
  "publisher": "Humdard Publisher"
}

Validation Rules

  • name β†’ required (string)
  • authors β†’ required (array of strings)
  • price β†’ required (number, must be β‰₯ 0)
  • publisher β†’ optional (string)

Responses

201 - Created

{
  "id": 1,
  "name": "Atomic Habits",
  "price": 100,
  "publisher": "Humdard Publisher",
  "authors": [{ "name": "Zain" }, { "name": "Chris" }]
}

400 - Bad Request

{
  "error": "Validation failed",
  "details": "Invalid input data"
}

Testing the API

You can test the API using:

  • Swagger UI β†’ http://localhost:3000/api-docs
  • Postman / Thunder Client

Notes

  • Ensure express.json() middleware is enabled
  • Request body must match validation schema

About

The project is mainly focus on practicing core skills like Project Mangement skill, swagger Api documentation, Express Project template for nodejs application, Git, Github RuleSet, Github Issues, Github Development team

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors