Coder Social home page Coder Social logo

typescript-express-docker's Introduction

#Typescript + Docker + Express + Mongoose = Awesome

codeship

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

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

Add new typings:

  • docker-compose run api typings install <wathever>

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

typescript-express-docker's People

Contributors

aherve avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.