Node (TypeScript+MongoDB) Starter Project for Startups.

See Project on GitHub

This is a free project to help startups quickly get up and running with a Node/TypeScript/MongoDB project. The concepts and methods used have been tested and validated in successful startups and industrial projects I have worked on.

The philosophy of this base project is to separate the code into three core folders: controllers which have the main logic, models folder which has all the database schema, and a data folder which has all the functions to access the database. The data folder model can be replaced with any database and this will not affect the functionality of the controllers and the tests.

Each controller has smaller independent service folders, each folder carrying similar logic to maximize reuse. Each controller folder name must end with an “ing” like “registering, listing, deleting …”. Each folder must contain at least three files.

  • service.ts: This contains all the functions with the logic in this folder.
  • sevice.test.ts: This contains the test of all the functions written in service.ts.
  • errors.ts: This contains all the errors which are used in the service.ts logic.
  • validate.ts(optional): This folder has all the logic to validate the data received over the REST API.

Find below the folder structure of the project with explanations.

Project structure

  • postman: all postman files should be added here.
    • nodebase.postman_collection.json postman collection for integration testing.
  • src all source code should be added in here.
    • controllers  – all core logic for endpoint control is added here.
      • registering: Sub-controller handling all registration logic.
        • errors.ts All errors used within the subcontroller.
        • service.ts All the logic for the sub-controller should be added here.
        • service.test.js Tests for all sub-controller functions should be added here. See the documentation for jest testing here.
        • validate.ts: all validation logic.
    • data all database access methods are added here.
    • models All database schemas for MongoDB 
    • routes All routes contained in the repo. 
    • seeders All seeds for the database
    • utils Any reusable typescript code, constants and errors are added here.
    • index.ts entry point of application.
    • connectmon.ts. service which connects to mongodb
  • .env.example: Contains all the env variables used in the project. Copy and rename to .env to use locally.
  • .gitlab-ci.yml: Gitlab CI/CD configuration file.

Technologies and Resources

  • Node.js
  • Express.js
  • TypeScript
  • MongoDB
  • Postman
  • Jest
  • Docker/Docker Compose
  • Git

Setup

  1. Download and Install your variant of MongoDB from here.
  2. Open the app and create a database called nodebase.
  3. Install Node.js. Download your variant from here
  • After install, open git bash or a terminal and type the command below to see if you get the help page
npm -h
  1. Clone this repository
git clone https://github.com/scneba/node_typescript_base.git
  1. open git bash or terminal on the root folder and run
npm install
  1. Make a copy of the .env.example and rename it to .env
  2. Update the connection string on line 2 to include your mongodb connection string
MONGO_URL="mongodb://localhost:27017/nodebase"
  1. Run npm run dev on the root folder to start the project.
  2. Install postman and create an account. Download from here.

Run Seeds

  • Update the connection string on line 19, src/seeder/seeder.ts to your mongodb connection string.
  • Run this command in the root folder to run the seeds on the database
npx tsnd src\seeder\seeder.ts

Testing

Ideally, all core logic in controllers should be tested. To test a particular sub-controller file, use the testPathPattern eg.

npm test -- --testPathPattern "registering/"

This will run all tests in the registering/ path.

Postman

Import the collection at Postman/nodebase.postman_collection.json into postman.

  • Update the domain variable in postman 
  • Start project with ts-node
npm run dev
  • Run postman collection and make sure all tests pass 

Congratulations, you are ready to start development with this base project.

Love what you read? Share below

Leave a Reply

Your email address will not be published. Required fields are marked *