All Projects → jbutko → express-ts-api-boilerplate

jbutko / express-ts-api-boilerplate

Licence: MIT license
Express TypeScript API Boilerplate

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to express-ts-api-boilerplate

express-mongoose-es8-rest-api
A Boilerplate for developing Rest api's in Node.js using express with support for ES6,ES7,ES8 ,Mongoose,JWT for authentication,Standardjs for linting
Stars: ✭ 20 (+33.33%)
Mutual labels:  dotenv, nodemon, joi
teaching-nodejs-expressjs-framework-spring-2019-2020
Complete Node-Express Application
Stars: ✭ 16 (+6.67%)
Mutual labels:  nodemon, express-js, nodejs-server
rest-api-node-typescript
This is a simple REST API with node and express with typescript
Stars: ✭ 154 (+926.67%)
Mutual labels:  nodemon, joi, joi-validation
Prettier Tslint
Code ➡️ prettier ➡️ tslint --fix ➡️ Formatted Code ✨
Stars: ✭ 221 (+1373.33%)
Mutual labels:  tslint, prettier
Nodejs Shopping Cart
NodeJS Shopping Cart - NodeJS, Express, Express-Session, Express handlebars (hbs)
Stars: ✭ 144 (+860%)
Mutual labels:  nodemon, express-js
web-build-tool-demo
比较全面的前端构建工具demo,拿来即练,不定时更新,包含git npm gulp webpack babel typescript husky eslint tslint lint-stage prettier等
Stars: ✭ 42 (+180%)
Mutual labels:  tslint, prettier
Typescript Starter
Quickly create and configure a new library or Node.js project
Stars: ✭ 2,953 (+19586.67%)
Mutual labels:  tslint, prettier
Tslint Plugin Prettier
Runs Prettier as a TSLint rule and reports differences as individual TSLint issues
Stars: ✭ 232 (+1446.67%)
Mutual labels:  tslint, prettier
twitch-chat-visualizer
A Node.js Project. Would you like to see your chat stream with a custom design? This is for you!
Stars: ✭ 14 (-6.67%)
Mutual labels:  dotenv, nodemon
api-with-express
🌈 API with Express
Stars: ✭ 25 (+66.67%)
Mutual labels:  prettier, joi
Registration-and-Login-using-MERN-stack
Simple Registration and Login component with MERN stack
Stars: ✭ 210 (+1300%)
Mutual labels:  nodemon, express-js
hackerAPI
🐓 API for registration, live-site
Stars: ✭ 20 (+33.33%)
Mutual labels:  api-rest, nodejs-server
boilerplate-express-api
🔋Boilerplate Express API w/ Support for ES6 Features
Stars: ✭ 22 (+46.67%)
Mutual labels:  express-js, nodejs-server
Aurora
📦 A set of configs and rules for Typescript projects.
Stars: ✭ 30 (+100%)
Mutual labels:  tslint, prettier
ExpressJS-SocketIO-Boilerplate
📦 Simple Express.js & Socket.io Boilerplate
Stars: ✭ 31 (+106.67%)
Mutual labels:  express-js, nodejs-server
spec
🐣 easy to use eslint/stylelint/tslint/prettier/...
Stars: ✭ 60 (+300%)
Mutual labels:  tslint, prettier
alda
A boilerplate for React isomorphic aplication with Material Design
Stars: ✭ 16 (+6.67%)
Mutual labels:  tslint, prettier
toi
A TypeScript validation library capable of inferring types
Stars: ✭ 25 (+66.67%)
Mutual labels:  joi, joi-validation
koa-restful-boilerplate
A boilerplate for koa2 RESTful API development
Stars: ✭ 31 (+106.67%)
Mutual labels:  joi
typescript-api-starter
🔰 Starter for Node.js express API in Typescript 🚀
Stars: ✭ 72 (+380%)
Mutual labels:  prettier

Express TypeScript API Boilerplate

Simple Express API boilerplate built with TypeScript.

Motivation

Main goal was to create a boilerplate for NodeJS projects built with TypeScript without having to spend long hours to setup development tools, build, linting and formatting tasks.

Technologies and Packages used

TypeScript, ES6, Express, TSLint, Dotenv, Prettier, Joi, Nodemon

Installation

git clone [email protected]:jbutko/express-ts-api-boilerplate.git
cd express-ts-api-boilerplate
yarn install // or npm install

Scripts

yarn run dev

  • run the app in development mode, app will be reloaded on file changes

yarn run start

  • start the app in non-reloadable mode

yarn run build

  • build the app

yarn run lint

  • check typescript errors via TSLint

yarn run lint:fix

  • check and fix typescript errors via TSLint

yarn run format

  • check for formatting errors via Prettier

yarn run format:fix

  • fix formatting errors via Prettier

yarn run format:lint:fix

  • check and fix typescript errors via TSLint and correct formatting errors via Prettier

Project directory structure example


│   .editorconfig                         // https://editorconfig.org/
│   .env                                  // Base environment variables goes here. BEWARE: This one is commited in the repo, do not store sensitive variables here.
│   .env.local                            // Sensitive ENV variables goes here. Overrides variables from `/.env`. BEWARE: This one is not commited in the repo.
│   .gitignore                            // Git ignored files
│   .prettierignore                       // Prettier ignored files
│   .prettierrc                           // Prettier config file: no more tab/space bullshit with your collegues
│   LICENSE                               // License file, MIT of course
│   nodemon.json                          // Nodemon config file
│   package.json                          // App dependencies, project information and stuff...
│   pm2-process.json                      // PM2 process config file (start the app with command `pm2 start pm2-process.json`)
│   README.md                             // Project Readme
│   tsconfig.json                         // TypeScript config file
│   tslint.json                           // TypeScript linting config file
│   yarn.lock                             // Yarn lockfile => https://yarnpkg.com/blog/2016/11/24/lockfiles-for-all/
│
└───src                                   // App root folder
    │   index.ts                          // Main entry point: http server and express app initialization
    │
    ├───app                               // App folder
    │   │   App.routes.ts                 // Main express router: individual routers from `app/routers` folder are imported here
    │   │   App.ts                        // Express app config: middlewares, router initialization, error handling initialization
    │   │
    │   ├───components                    // All components (entities) goes here
    │   │   └───Common                    // Common component example
    │   │           Common.controller.ts  // API controller for `Common` component: API endpoint handlers goes here, keep it simple!
    │   │           Common.validators.ts  // Joi validation schemas. Imported in `app/routers` files.
    │   │           Common.interface.ts   // TypeScript interfaces/enums for `Common` component
    │   │           Common.db.ts          // Database access related code
    │   │           Common.service.ts     // Generic functions related to data processing or stuff that do not need db access
    │   │           Common.middleware.ts  // Express middleware functions: for example user auth token verification etc. Imported in `app/routers` files.
    │   │           index.ts              // Public API of `Common` component: CommonController, commonValidators etc.
    │   │
    │   ├───core                          // Core components: common logic that is used in more than one place of the app
    │   │       Env.ts                    // Environment settings configuration through dotenv - variables from `/.env` and `./env.local` will be initialized here.
    │   │       ErrorHandling.ts          // Express error handler functions for prod/dev
    │   │       Dates.ts                  // All dates/times related functions
    │   │       index.ts                  // Public API of `core` folder: ENV, ErrorHandling etc.
    │   │
    │   └───routers                       // Routers for individual components from `components` folder
    │           Common.router.ts          // API Endpoint handlers for `Common` controller
    │           index.ts                  // All exported routers
    │
    └───types                             // TypeScript definition files goes here
            types.d.ts                    // Generic typescript definition file

Debugging

If you need to debug some of your code during development, it's very easy. Open following URL in Chrome: chrome://inspect/#devices. Click on Open dedicated DevTools for Node => DevTools should open. Use Ctrl + P shortcut to find the file you need, for example Common.controller.ts. After adding a breakpoint the TypeScript file should be opened directly in devtools.

Note: If inspect mode does not work for you, you need to configure ports by clicking on Configure button in chrome://inspect/# devices. The websocket port through which the inspect mode works is displayed during app launch in the command line ("Debugger listening on ws://127.0.0.1:9229/..."). In this example you need to add localhost:9229 in Configure settings.

API Endpoints params validation

To validate input params sent from API user Joi package is used. At first you need to define validation schemas (example). The next step is to import schemas in your router, instantite Joi validator and use it as middleware. This way you can separate params validation logic out of controllers. Check the example.

Production deployment example

Clone the repo on any unix (cloud) server. Make a build of the app:

yarn run build

Install pm2 process manager:

yarn add -g pm2

Start the app:

pm2 start pm2-process.json

App will be started in daemon mode (background). To check the logs of the app issue following command:

pm2 logs nameOfTheAppFromPm2

You can find the name of the app in pm2-process.json file.



Copyright (C) 2019 Jozef Butko www.jozefbutko.com

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].