This project is a fully working bootstrap for developping an express-mongoose api with typescript.
- 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 ofsend()
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.
- 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
sudo docker-compose -f docker-compose.prod.yml up
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
docker-compose run api npm install <wathever> --save
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.
Typescript validations are awesome:
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)
})
The config provided in codeship-services.yml
and codeship-steps.yml
will trigger a build at each code commit.
- automated build of a production-ready docker