0.0.1 • Published 6 years ago

@rippell/ts-express v0.0.1

Weekly downloads
1
License
ISC
Repository
gitlab
Last release
6 years ago

TS-Express

Start a quick and dirty Express JS server that live-interprets Typescript, scanning your project for controllers.

//TODO: - Watch and reload files - Plugin structure (add support for things like GraphQL) - Optionally enable Mongoose (peer dependency installation of course) - Testing.......

Install

npm i @rippell/ts-express -D

Then add a execution to package.json scripts.

    ...
    "scripts": {
      "start": "ts-express",
      "debug": "ts-express-debug"
    }
    ...

ts-express-debug starts node with --inspect to allow debugging (go to chrome://inspect and click Open dedicated DevTools for Node).


Then all .ts files under /src will be found by default. All files that export a endpoint object will attempt to be registered with the Express Router.

Endpoint object should adhere to the EndpointSet interface. Here's an example:

import { EndpointSet } from "@rippell/ts-express";

export const endpoints: EndpointSet = {
  '/v1/my-endpoint': {
    get:    (req, res) => { return   { cool:     'beans!'  } }, // Can Return an object
    post:   (req, res) => { res.json({ also:     'neat!'   })}, // Or send it yourself
    put:    (req, res) => { return   { too:      'many'    } },
    patch:  (req, res) => { return   { examples: 'to'      } },
    delete: (req, res) => { return   { cover:    'cleanly' } },
  },
  '/v1/another': {
    get: (req, res) => { return { moar: 'endpoints!' } }
  }
}

Optionally add a tsexpress.ts file at your project root, exporting a config: TSExpressConfig to configure TS-Express.

import { TSExpressConfig } from '@rippell/ts-express';

export const config: TSExpressConfig = {
  port: 9999
}

See Configuration below for more.

Out of the box

Express

Express is configured with:

Configuration

The following configuration is used out of the box:

{
    fileGlob:  'src/**/!(*.d).ts', // Any files under /src that end in .ts (but not .d.ts)
    host:      '0.0.0.0',          // Start and expose (convenience for containers or container-based IDEs like Eclipse-CHE)  
    port:      8080,               // Expose on 8080
    watch:     false,              // Don't set up watches by default
    verbose:   false               // Don't flood logs with TS-Express logging
}

See all available configuration in the TSExpressConfig interface.

As mentioned, a tsexpress.ts may be supplied at the project root, but all settings are overridden by environment variables in support of containerization. See env object for which values are pulled from the environment.