Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
middleware
routes
typings
.gitignore
README.md
application.ts
gulpfile.js
index.ts
package.json
tsconfig.json
typings.json

README.md

to get this up and running

npm install
typings install
npm start

tsconfig.json

For configuring the behaviour of the typescript compiler

{
    "compilerOptions": {
        "module":"umd",
        "sourceMap": true,
        "noEmitOnError": true,
        "strictNullChecks": 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 );
    }

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

routes/customerRouter.ts

Example express Router implementation

import express = require("express")

let router = express.Router();
router.get('/', (req, res) => {
    let testData = {
        firstName: "Jim",
        lastName: "Burger"
    }
    res.send(testData);
});

export = router;

middleware/requestLogger.ts

Example custom middleware

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}`);
You can’t perform that action at this time.