Boilerplate code to get started with building RESTful API Services (Express, TypeORM MongoDB stack)
Warning
This project is no longer being actively maintained and is therefore likely to be outdated.
Awesome boilerplate code to get started with building RESTful API Services! Typescript with NodeJS and Express as well as MongoDB integration with TypeORM JSON Web Tokens (JWT) based User Authentication, TypeDI dependency injections, testing with Jest, and a bunch more. Made with ❤️ by Brian Min
The main reason I decided to start this project is to provide a boilerplate for the NodeJS, Express, Typescript, TypeORM, MongoDB stack. Also with TypeORM, this boilerplate code can easily be modified to use MySQL, PostGreSQL, and many more. Tedious setting up of project structure, logging, testing, env and eslint is all done for you! Now you can focus on actually implementing the core functionalities and not spending hours setting up and configuring your project.
Try it out and feel free to raise any issues or create pull requests if you would like to contribute!
Install Node.js and NPM
Install a MongoDB server or use MongoDB Atlas
Fork or download this project and modify package.json
for your new project.
Make a copy of the .env.example
file and rename it to .env
.
Create a new database and add the connection string in the .env
file.
Install the required packages.
npm install
This installs all the dependencies with NPM.
Now your development environment should be ready to use!
Go to the root directory and start your app with this npm script.
npm run dev
This starts a local server using
nodemon
andts-node
. The server base endpoint will behttp://127.0.0.1:3000
where3000
is the PORT variable you set in the.env
file.
npm install
npm run lint
which runs eslint.npm run lint:fix
.For this step you need to install MongoDB Server
npm run mongodb
to start a local MongoDB server with it's data stored in .mongodb
in the root directory.npm run test
(for Windows users) or npm run test:unix
(for Mac and Linux users).npm run dev
to start nodemon with ts-node.http://127.0.0.1:3000
where 3000
is the PORT variable you set in the .env
file.npm run build
to compile all the Typescript sources and generate JavaScript files.build
use npm start
.The route prefix is /api
by default, but you can change this in the .env file.
Route | Description |
---|---|
/api | Base endpoint |
/api/auth/login | Auth - login endpoint |
/api/auth/register | Auth - register endpoint |
/api/user | Example entity endpoint - returns all users |
/api/user/current | Example entity endpoint - returns current logged in user |
/api/company | Example entity endpoint - returns all companies |
/api/company/:id | Example entity endpoint - returns a company by id |
/api/jobApplication | Example entity endpoint - returns all job applications |
... | ... |
Name | Description |
---|---|
.mongodb/ | Local MongoDB server data |
build/ | Compiled source files will be placed here |
commands/ | Custom CLI command tools used with npm scripts |
src/ | Source files |
src/api/middlewares/ | Custom middlewares |
src/api/entities/ | TypeORM Entities (Database models) |
src/api/services/ | Service layer |
src/config/ | The configuration file which loads env variables |
src/database/factories | Factories generate entities with mock data |
src/database/seeds | Seeds use factories to save mock data in the database |
src/loaders/ | Loader is where the app is configured and database is loaded |
src/types/ *.d.ts | Custom type definitions |
test *.spec.ts | Unit and integration tests |
.env.example | Environment configurations |
For logging we use winston.