/broker-api

typed broker

Primary LanguageTypeScript

Typescript + Docker + Express + Mongoose = Awesome

Codeship Status for aherve/typescript-express-docker

About this

This project is a fully working bootstrap for developping an express-mongoose api with typescript.

Why is it awesome ?

  • the ONLY install you need is docker. Don't worry about installing mongo, npm or whatever else. Docker-compose takes all the leverage for you (see Installation).
  • Typescript is awesome. your code is checked against a LOT of possible mistakes. Try inserting some typos such as ssend(...) instead of send() and see the console warn you about it.
  • Possible usage of await/async and/or Promise-style code => no Callback Hell
  • Linters are already installed. Keep your code clean.
  • Automated mocha unit testing. Mocha tests are written in ts, but run on the transpiled javascript code. So the actual transpiled application is properly tested.
  • Automated testing by codeship on code commit. They run the same container as you do : if it works at home, it works on CI-servers.

Installation

  • clone the repo
  • run docker-compose up
  • browse http://localhost:3000/api/ping, http://localhost:3000/api/views/promise, http://localhost:3000/api/views/await
  • profit

Useful knowledge

for production build

sudo docker-compose -f docker-compose.prod.yml up

Rebuilding the images

After adding new npm or typings package, you will have to rebuild your images as follows:

  • docker-compose stop
  • docker-compose rm
  • docker-compose build
  • docker-compose up

Add new npm package:

  • docker-compose run api npm install <wathever> --save

Can I deploy ?

I'v not yet automated the deployment. For now you can do whatever you want with the /dist folder, that contains the transplied js code.

Typos

Typescript validations are awesome:

typo

selection_063

Await

Typescript now support async/await and this build let you take advantage of it. See /api/views for two implementations of a simple mongoose request. One uses await, whereas the other uses promises.

The code now looks like as it where synchronous, but does not block the thread:

export async function awaitIndex (req: express.Request, res: express.Response) {
  try {
    const lastVisit: IView = await View.create({
      visitedAt: new Date(),
      visitedBy: getIp(req),
    })

    const count: number = await View.count({})
    res.send({ count, lastVisit: lastVisit.public })

  } catch (error) {
    res.status(500).send({error: error.toString()})
  }
}

Even the tests are written using ES7-like syntax:

 it('returns a new view', async function () {
    const t = await request.get('/api/views/await')
    expect(t.status).to.equal(200)
    expect(t.body).to.have.property('lastVisit')
    expect(t.body).to.have.property('count', 1)
  })

Continuous Testing

The config provided in codeship-services.yml and codeship-steps.yml will trigger a build at each code commit.

TODO/ coming soon

  • automated build of a production-ready docker