Skip to content

md-abu-kayser/raw-node-typescript-crud-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

63 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Raw Node.js & TypeScript CRUD API

Node.js TypeScript License: MIT GitHub Stars

A professional, production-ready CRUD (Create, Read, Update, Delete) API built entirely with raw Node.js and TypeScript. This project demonstrates advanced server-side development techniques without relying on frameworks like Express, showcasing deep understanding of HTTP protocols, routing, and asynchronous programming.

πŸš€ Features

  • Framework-Free: Built with raw Node.js HTTP module for maximum control and minimal dependencies
  • TypeScript Integration: Full type safety with custom interfaces and type definitions
  • Custom Router: High-performance routing system supporting static and dynamic routes
  • Environment Configuration: Secure environment-based configuration using dotenv
  • JSON Database: Lightweight file-based database with atomic write operations
  • Comprehensive CRUD Operations: Complete user management API with validation
  • Error Handling: Robust error handling with proper HTTP status codes
  • Async/Await Support: Modern asynchronous programming patterns
  • Input Validation: Built-in validation for API inputs
  • Modular Architecture: Clean, maintainable code structure

πŸ›  Tech Stack

  • Runtime: Node.js 18+
  • Language: TypeScript 5.0+
  • HTTP Handling: Node.js built-in http module
  • Configuration: dotenv
  • Development: ts-node-dev, nodemon-like hot reloading
  • Testing: Manual testing with curl/Postman
  • Database: JSON file (easily replaceable with real databases)

πŸ“¦ Installation

  1. Clone the repository

    git clone https://github.com/md-abu-kayser/raw-node-typescript-crud-api.git
    cd raw-node-typescript-crud-api
  2. Install dependencies

    npm install
  3. Environment Setup (Optional) Create a .env file in the root directory:

    PORT=5000
    NODE_ENV=development
  4. Start the development server

    npm run dev

The server will start on http://localhost:5000 (or your configured PORT).

🎯 Usage

Development

npm run dev          # Start development server with hot reload
npm run build        # Compile TypeScript to JavaScript
npm run start        # Run production server
npm run typecheck    # Run TypeScript type checking

Testing the API

Use curl, Postman, or any HTTP client to test the endpoints.

Health Check

curl http://localhost:5000/health

Get All Users

curl http://localhost:5000/api/users

Create a New User

curl -X POST http://localhost:5000/api/users \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "country": "United States",
    "study": "Computer Science"
  }'

Get a Specific User

curl http://localhost:5000/api/users/1

Update a User

curl -X PUT http://localhost:5000/api/users/1 \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Jane Doe",
    "country": "Canada"
  }'

Delete a User

curl -X DELETE http://localhost:5000/api/users/1

πŸ“‹ API Endpoints

Method Endpoint Description Request Body
GET / Welcome message -
GET /health Server health check -
GET /api/users Get all users -
GET /api/users/:id Get user by ID -
POST /api/users Create new user {name, country, study}
PUT /api/users/:id Update user by ID {name?, country?, study?}
DELETE /api/users/:id Delete user by ID -

Request/Response Examples

Successful Response

{
  "success": true,
  "message": "User created successfully",
  "data": {
    "id": 1,
    "name": "John Doe",
    "country": "United States",
    "study": "Computer Science",
    "createdAt": "2024-01-01T00:00:00.000Z",
    "updatedAt": "2024-01-01T00:00:00.000Z"
  }
}

Error Response

{
  "success": false,
  "message": "User not found"
}

πŸ— Project Structure

raw-node-typescript-crud-api/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ server.ts           # Main server file
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── index.ts        # Environment configuration
β”‚   β”œβ”€β”€ data/
β”‚   β”‚   └── users.json      # JSON database file
β”‚   β”œβ”€β”€ helpers/
β”‚   β”‚   β”œβ”€β”€ router.ts       # Custom routing system
β”‚   β”‚   β”œβ”€β”€ sendJson.ts     # JSON response helper
β”‚   β”‚   β”œβ”€β”€ parseBody.ts    # Request body parser
β”‚   β”‚   β”œβ”€β”€ fileDb.ts       # File database operations
β”‚   β”‚   └── validators.ts   # Input validation utilities
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   └── index.ts        # API route definitions
β”‚   └── types/
β”‚       β”œβ”€β”€ http.ts         # HTTP type definitions
β”‚       └── user.ts         # User data types
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json
β”œβ”€β”€ .env                    # Environment variables (create if needed)
└── README.md

πŸ”§ Key Components

Custom Router

  • High Performance: O(1) route lookups using Map data structures
  • Dynamic Routes: Support for parameterized routes (e.g., /users/:id)
  • Method-Based Routing: Separate route maps for each HTTP method

File-Based Database

  • Atomic Operations: Uses temporary files to prevent data corruption
  • Error Recovery: Graceful handling of file read/write errors
  • Easy Migration: Simple to replace with SQL/NoSQL databases

TypeScript Integration

  • Type Safety: Comprehensive interfaces for all data structures
  • IntelliSense: Full IDE support with autocompletion
  • Compile-Time Checks: Catch errors before runtime

🀝 Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add amazing feature'
  4. Push to the branch: git push origin feature/amazing-feature
  5. Open a Pull Request

Development Guidelines

  • Follow TypeScript best practices
  • Add tests for new features
  • Update documentation
  • Use conventional commit messages

License

  • This project is licensed under the terms of the MIT License.
  • You may replace or update the license as needed for client or proprietary projects.

Contact and Maintainer

πŸ™ Acknowledgments

  • Node.js community for the amazing runtime
  • TypeScript team for the powerful type system
  • Open source contributors who make projects like this possible

⭐ Star this repo if you found it helpful!

For questions or support, please open an issue on GitHub.

About

Production-ready CRUD API built with raw Node.js and TypeScript, featuring custom routing, JSON file storage, and clean modular architecture.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors