/tsexpress-starter

Starter repository for TypeScript + Express.js

Primary LanguageTypeScriptMIT LicenseMIT

TS Express Starter

license

Express with TypeScript and more.

Install

Recommended installation
yarn global add @tsexpress-starter/cli

tsexpress-starter new my-project
Alternatively, use git clone
git clone https://github.com/dugajean/tsexpress-starter
Manage with TS Express Starter CLI
tsexpress-starter --help

Usage

Start off with a server.js in src/ with the following contents:

import express from 'express';
import Application from '@tsexpress-starter/application';

new Application(__dirname, express()).start();

A new folder within the src/app folder represents a module/domain. So to create your endpoints, simply use the CLI and run tsexpress-starter make:domain <my domain>, which then sets up a new folder within src/app for you.

Any method within the controller.ts file can be decorated with one of the HTTP verbs that suits your needs: Get, Post, Put, Patch and Delete. Each of these decorators accepts a path as a first argument and a list of middlewares after the first argument.

Example controller:

// src/app/sample/controller.ts

import { Get, Post } from '@tsexpress-starter/routes';
import fooMiddlewareFunc from '../../middleware/fooMiddlewareFunc';
import barMiddlewareFunc from '../../middleware/barMiddlewareFunc';
import bazMiddlewareFunc from '../../middleware/bazMiddlewareFunc';

export class Controller {
  @Get('/') // GET http://localhost:3000/sample
  home(req, res) {
    res.send('This is the home page.');
  }

  @Get('/posts', fooMiddlewareFunc, barMiddlewareFunc) // GET http://localhost:3000/sample/posts
  postsIndex(req, res) {
    res.send('This is where we list all posts.');
  }

  @Post('/posts', bazMiddlewareFunc) // POST http://localhost:3000/sample/posts
  createPost(req, res) {
    res.send('This is where we create a new post.');
  }
}

You're free to do anything you would do in a normal ExpressJS app. These features are all optional and can simply be ignored. Just use Express with Typescript.

Integration with TypeORM

If you want to connect to a database with this "framework", I would suggest using TypeORM. There's also a helper package that makes the integration with TypeORM quite easy.

Find out more about it here: https://www.npmjs.com/package/@tsexpress-starter/typeorm

Tests

Write your own tests and run them with:

$ yarn test

License

TSExpress Starter is released under the the MIT License.