Skip to content

kenfj/express-mongo-tsoa-todo-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Express MongoDB TSOA Todo REST API Example

Node.js REST API sample code using followings

  • Node.js & Express
  • MongoDB & Mongoose
  • tsoa & swagger-ui-express
  • Jest & Supertest & mongodb-memory-server for Unit test
  • All in TypeScript

Sample API

  • hello: Simple and Minimalism sample API
  • todos: TSOA and Swagger based simple Todo CRUD

Setup

cd server
npm init --yes

npm install --save-dev typescript @types/node
npx tsc --init

npm install --save-dev eslint
npx eslint --init
# this will suggest to install
# @typescript-eslint/parser @typescript-eslint/eslint-plugin

npm install --save-dev jest @types/jest ts-jest
npx ts-jest config:init

npm install express
npm install --save-dev @types/express

npm install --save-dev tsc-watch # c.f. ts-node-dev
npm install --save-dev supertest @types/supertest

npm install mongoose
npm install --save-dev @types/mongoose

# In-Memory MongoDB
# https://github.com/nodkz/mongodb-memory-server
npm install --save-dev mongodb-memory-server

npm install tsoa swagger-ui-express
npm install --save-dev @types/swagger-ui-express
npx tsc --init

Run Server and Test

npm run dev
open http://127.0.0.1:3000/

curl 127.0.0.1:3000/hello?name=Alice
curl -X POST 127.0.0.1:3000/hello -d "name=Bob"
curl 127.0.0.1:3000/hello

npm run test

npm run test:coverage
open coverage/lcov-report/index.html

REST API Framework TSOA + Swagger Doc

npm run dev

# run test from swagger doc
open http://127.0.0.1:3000/docs

Reference