Here lies a simple(ish) pattern to build express API’s with typescript.

Why typescript for the server?

For the same reason people end up using other transpiled languages! Because I get an early warning system - for very little upfront effort. Also, it allows me use a language that reminds me of the best parts of C#, F# and javascript.

So I don’t need to write unit tests anymore?

Sorry but even though this example doesn’t come packing with tests, you should probably write tests for your production code. Not all issues are picked up by type checking.

Take a look on github

What next?

You could easily extend this example to include authentication, produce dynamic api documentation, add a database and many other types of middleware.

Break it down…

tsconfig.json

For configuring the behaviour of the typescript compiler

{
    "compilerOptions": {
        "module":"umd",
        "sourceMap": true,
        "noEmitOnError": true
    },
    "exclude": [
        "node_modules/",
        "typings/"
    ]
}

NOTE You’ll want to use typings to bring in nodejs & express definitions.

application.ts

Application Class

///<reference path="./typings/main.d.ts"/>
import express = require("express");
import customerRouter = require("./routes/customerRouter");
import requestLogger = require("./middleware/requestLogger");

export class WebApi {
    /**
     * @param app - express application
     * @param port - port to listen on
     */
    constructor(private app: express.Express, private port: number) {
        this.configureMiddleware(app);
        this.configureRoutes(app);
    }

    /**
     * @param app - express application
     */
    private configureMiddleware(app: express.Express) {
        app.use(requestLogger);
    }

    private configureRoutes(app: express.Express) {
        app.use("/customer", customerRouter );
        // mount more routers here
        // e.g. app.use("/organisation", organisationRouter);
    }

    public run() {
        this.app.listen(this.port);  
    }
}

routes/customerRouter.ts

Example express Router usage

import express = require("express");

let customerRouter = express.Router();
customerRouter.get('/', (request: express.Request, response: express.Response) => {
    let testData = {
        firstName: "The",
        lastName: "Burge"
    }

    response.send(testData);
});
// add more route handlers here
// e.g. customerRouter.post('/', (req,res,next)=> {/*...*/})
export = customerRouter;

middleware/requestLogger.ts

Example custom middleware implementation

import express = require("express");

let requestLogger: express.RequestHandler = (
    request: express.Request,
    response: express.Response,
    next: express.NextFunction
) => {
    console.info(`${(new Date()).toUTCString()}|${request.method}|${request.url}|${request.ip}`);
    next();
}

export = requestLogger;

index.ts

Program entry point

import express = require('express');
import { WebApi } from './application';

let port = 5001; //or from a configuration file
let api = new WebApi(express(), port);
api.run();
console.info(`listening on ${port}`);