Work-Stufff / RestfulAPI

Build a Restful API with Nodejs Express and Mongodb Typescript ๐Ÿ‘‰ C.R.U.D, Filter, Paginate, Sort and Search API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build a Restful API | Nodejs + Express + Mongodb

  • C.R.U.D, Filter, Paginate, Sort and Search API

Author: Dev A.T Viet Nam

Getting started: npm install -> npm run dev

๐Ÿ”ฅ Donate

๐Ÿ‘ป๐Ÿ‘ปVietNam:

  • ๐Ÿ‘‰Vietcombank: 0061001044348 (LE TUAN ANH)
  • ๐Ÿ‘‰Momo : 0374481936

I - Setup

  1. npm i express.
    - A web framework for node

  2. npm i mongoose
    - A Object Document Mapper - ODM for MongoDB

  3. npm i cors
    - CORS is a node.js package for providing a Connect/Express middleware

  4. npm i dotenv
    - Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env

  5. npm i morgan
    - HTTP request logger middleware for node.js

  6. npm i -D typescript
    - TypeScript is JavaScript with syntax for types

  7. npm i -D ts-node-dev
    - It restarts target node process when any of required files changes

  8. Create a "tsconfig.json" file and copy:

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "rootDir": "src"
  },
  "lib": ["es2015"]
}
  1. Updating the package.json File

II - How to use?

  1. Routes
> GET    /api/products
> GET    /api/products/:id
> POST   /api/products
> PUT    /api/products/:id
> DELETE /api/products/:id
  1. Filter
    gt = greater than, gte = greater than or equal.
    lt = lesser than, lte = lesser than or equal.
> GET /api/products?price=15
> GET /api/products?price[gt]=15.99
> GET /api/products?price[gte]=15.99
> GET /api/products?price[lt]=15.99
> GET /api/products?price[lte]=15.99
> GET /api/products?title[regex]=men&price[lte]=15.99
  1. Paginate
    Default page=1 and limit=5
> GET /api/products?page=2
> GET /api/products?page=2&limit=7
  1. Sort
    Default sort='-createdAt'
> GET /api/products?sort=price
> GET /api/products?sort=createdAt
  1. Full-text search
> GET /api/products?search=men

About

Build a Restful API with Nodejs Express and Mongodb Typescript ๐Ÿ‘‰ C.R.U.D, Filter, Paginate, Sort and Search API.


Languages

Language:TypeScript 100.0%