Mongoose Node.js Express TypeScript application boilerplate with best practices for API development.

Overview

Node TypeScript Boilerplate Mongoose

The main purpose of this repository is to show a good end-to-end project setup and workflow for writing a Mongoose Node.js Express code in TypeScript complete with middleware, models, routes, and types.

This example comes with a complete REST API to handle Authentication and CRUD features on Users and their corresponding Profile.

Tech Stack

Server: Node, Express, typescript, ts-node

DataBase: mongoose

Run Locally

Clone the project

  git clone https://github.com/chiragmehta900/node-typescript-boilerplate-mongoose

Go to the project directory

  cd node-typescript-boilerplate-mongoose

Install dependencies

  npm install

Start the tsc

  npm run watch

Start the server

  npm run dev

Environment Variables

To run this project, you will need to add the following environment variables to your .env file

NODE_ENV="production", NODE_ENV="local", MONGO_DB_USER, MONGO_DB_PASSWORD. JWT_SECRETS, SMTP_HOST, SMTP_PORT, SMTP_USERNAME, SMTP_PASSWORD, SMTP_SENDER,

Project Structure

The most obvious difference in a TypeScript + Node project is the folder structure. In a TypeScript project, it's best to have separate source and distributable files. TypeScript (.ts) files live in your src folder and after compilation are output as JavaScript (.js) in the dist folder.

The full folder structure of this app is explained below:

Note! Make sure you have already built the app using npm run start

Name Description
config Contains config environment to be used by the config package, such as MongoDB URI, jwtSecret, and etc.
dist Contains the distributable (or output) from your TypeScript build
node_modules Contains all your npm dependencies
src Contains your source code that will be compiled to the dist dir
src/middleware Contains the middlewares to intercept requests
src/models Models define Mongoose schemas that will be used in storing and retrieving data from MongoDB
src/controller The controller accesses the database through the model
src/routes Routes define the endpoints of your API
src/interfaces Contains all your custom types to better handle type checking with TypeScript
src/services Contains custom types of services like sending mail and etc..
src/templates Contains custom mail templates of send mail for users.
src/index.ts Entry point to your express app
package.json File that contains npm dependencies as well as build scripts
tsconfig.json Config settings for compiling server code written in TypeScript
tslint.json Config settings for TSLint code style checking

Configuring TypeScript compilation

TypeScript uses the file tsconfig.json to adjust project compile options. Let's dissect this project's tsconfig.json, starting with the compilerOptions which details how your project is compiled.

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "lib": [
      "es2016",
      "dom"
    ],
    "allowJs": true,
    "checkJs": true,
    "declaration": true,
    "sourceMap": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "removeComments": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "pretty": true,
    "experimentalDecorators": true,
    "typeRoots": [
      "node_modules/@types"
    ]
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

Documentation

Documentation

API Reference

Create user

  POST user/sign-up
Parameter Type Description
firstName string Required. Your API key
lastName string Required. Your API key
avatar string Required. Your API key
email string Required. Your API key
password string Required. Your API key
confirmPassword number Required. Your API key

login user

  POST auth/login
Parameter Type Description
email number Required. Your API key
password string Required. Your API key

Get all users

  GET user/fetch
Parameter Type Description
_id uuid Required. Your API key
firstName string Required. Your API key
lastName string Required. Your API key
gender string Required. Your API key
dateOfBirth string Required. Your API key
residence string Required. Your API key
avatar string Required. Your API key
email string Required. Your API key
password string Required. Your API key
role string Required. Your API key
isEmailVerified boolean Required. Your API key
isEmailVerified boolean Required. Your API key

A postman collection has been added for better understanding.

Badges

Add badges from somewhere like: shields.io

MIT License

GPLv3 License

AGPL License

Author

Support

For support, email [email protected] or join our Slack channel.

You might also like...

School App / Back-End with MongoDB / mongoose / Express / TS

TEST Api Dependencies El mati se la come es por eso que en 1998 la guerra fria se llevo a mas de la mitad del activo del pais "dependencies": { "axios

Jun 10, 2022

A set of best practices for JavaScript projects

A set of best practices for JavaScript projects

中文版 | 日本語版 | 한국어 | Русский | Português Project Guidelines · While developing a new project is like rolling on a green field for you, maintaining it is

Jan 1, 2023

An easy-to-read, quick reference for JS best practices, accepted coding standards, and links around the Web

An easy-to-read, quick reference for JS best practices, accepted coding standards, and links around the Web

Feel free to contribute! Where? http://www.jstherightway.org Why? Today we have a bunch of websites running JavaScript. I think we need a place to put

Jan 1, 2023

Enforcing best practices for Effector

eslint-plugin-effector Enforcing best practices for Effector This plugin uses TypeScript for more precise results, but JavaScript is supported too. In

Dec 15, 2022

Some HTML, CSS and JS best practices.

Frontend Guidelines HTML Semantics HTML5 provides us with lots of semantic elements aimed to describe precisely the content. Make sure you benefit fro

Jan 1, 2023

A solid create-remix app, that applies best practices into a clean, batteries included template. SQLite version. Deploys to Fly.io

A solid create-remix app, that applies best practices into a clean, batteries included template. SQLite version. Deploys to Fly.io

Remix Barebones Stack npx create-remix --template dev-xo/barebones-stack A solid create-remix app, that follows community guidelines and applies best

Dec 30, 2022

In this project, I implement a Simple To Do List with the CRUD (create, read, update, delete) methods. I followed the JavaScript, CSS, HTML, DRY, KISS and YAGNI Best practices.

In this project, I implement a Simple To Do List with the CRUD (create, read, update, delete) methods. I followed the JavaScript, CSS, HTML, DRY, KISS and YAGNI Best practices.

To Do list: add & remove In this project, I implement a Simple To Do List with the CRUD (create, read, update, delete) methods. All the elements of th

Nov 20, 2022

In this project, I built a to-do list app, which is inspired by the minimalist website. Build withHTML/CSS & JavaScript best practices: Correct use of tags, elements, properties and syntax.

Webpack Exercise In this project, I built a to-do list app, which is inspired by the minimalist website. Built With HTML/CSS & JavaScript best practic

Oct 11, 2022

Do i follow JS best practices?

To-Do-List-code-review Do i follow JS best practices? Built With HTML CSS JavaScript ES6 Webpack Live Demo (https://kelvin-ben.github.io/To-Do-list/)

Oct 10, 2022
Owner
Chirag Mehta
Hi, I m Chirag Mehta, a professional MERN - Stack developer with 3 years of experience.
Chirag Mehta
Create a maintainable and scalable Node.js GraphQL API with TypeScript, Express, Mongoose and Apollo Server.

Set up and build a Node.js GraphQL API using Typescript, Express, Mongoose with a maintainable and scalable structure

Adam Khomsi 7 Nov 4, 2022
Best practices for modern web development

Web Fundamentals on DevSite Welcome to the new WebFundamentals! An effort to showcase best practices and tools for modern Web Development. What's chan

Google 13.7k Dec 30, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
An IoT bottle that tracks water consumption. Winner of Best Health Hack, MLH's Best Hardware Hack, and QB3's Best Big Data for the Improvement of Health Care Winner at CruzHacks 2022.

An IoT bottle that tracks water consumption. Winner of Best Health Hack, MLH's Best Hardware Hack, and QB3's Best Big Data for the Improvement of Health Care Winner at CruzHacks 2022.

Nathanael Garza 2 Jan 21, 2022
Algorithms and Data Structures implemented in TypeScript for beginners, following best practices.

The Algorithms - TypeScript TypeScript Repository of TheAlgorithms, which implements various algorithms and data structures in TypeScript. These imple

The Algorithms 166 Dec 31, 2022
:white_check_mark: The Node.js best practices list (March 2021)

Node.js Best Practices Follow us on Twitter! @nodepractices Read in a different language: CN, BR, RU, PL, JA, EU (ES, FR, HE, KR and TR in progress! )

Yoni Goldberg 85k Jan 9, 2023
📗🌐 🚢 Comprehensive and exhaustive JavaScript & Node.js testing best practices (August 2021)

?? Why this guide can take your testing skills to the next level ?? 46+ best practices: Super-comprehensive and exhaustive This is a guide for JavaScr

Yoni Goldberg 19.9k Jan 2, 2023
REST API built with Node.js, MongoDB, Mongoose & TypeScript. I also dabbled with Docker and Caddy for deployments

REST API with Node.js, Mongoose & TypeScript Note: This repository includes the postman collection for the finished API Note 2: Make sure you add .env

Manuchimso Oliver 2 Sep 16, 2022