Giter Site home page Giter Site logo

tracks-boot's Introduction

tracks-boot (@ianwremmel/tracks-boot)

license standard-readme compliant npm (scoped) npm

Dependabot badge semantic-release

CircleCI

@ianwremmel/tracks-boot is a tiny module to start Express apps asynchronously

Most tutorials for Express show how to get up and running quickly with a synchronous example. Unfortunately, the reality is that sometimes we need to do async work before we can start listening for connections. Maybe we need to load config from a file or wait for database connections. Maybe we're pulling config from a remote store like Consul. In any case, it can get pretty clunky. This module aims to streamline that clunkiness as much as possible.

Table of Contents

Install

npm install @ianwremmel/tracks-boot

Usage

tracks-boot exports two functions, prepare for doing all of the async work of preparing your application and boot for binding it to a port. These are separate functions so that you can use e.g. supertest against a prepare app without needing to test over http.

First, import tracks boot

import {boot, prepare} from '@ianwremmel/tracks-boot';

Next, create your app factory (yes, I said "factory"; it's not that bad. it's just a function).

export function createApp() {
    return prepare(async (app) => {
        // do all of your normal app setup here. add middleware, routes, etc.
        app.get('/ping', (req, res) => {
            res.send('It Works!');
        });

        // This is an async function, so you can use await anywhere you need to.
    });
}

Now, define your boot behavior. You could put this in the same file as your app factory and rely on testing require.main === module to only start the app when appropriate or you could use a separate file. The example below assumes everything goes in the same file. This example just confirms the environment is set correctly to bind to a port. (I recommend doing most other env-var checking in the app factory rather than here. PORT is checked here because it's explicitly needed by boot()).

if (require.main === module) {
    if (!process.env.PORT) {
        throw new Error('PORT is not defined');
    }

    const port = Number(process.env.PORT);

    if (Number.isNaN(port)) {
        throw new Error('PORT must be a number');
    }

    // we'll just pass console as our logger, but if you're using something more
    // complex like bunyan, that's fine too. Just make sure it implements
    // `info: (arg: string) => void`
    boot({logger: console, port}, create());
}

boot returns a Promise<http.Server>, so if you need to shut it down programmatically (vs killing the process), you can use e.g. boot(...).then((server) => server.close());

Finally, we can use supertest to test our app without binding it to a port

import {createApp} from '.';

it('responds to pings', async () =>
    supertest(await createApp())
        .get('/api/v1/healthcheck')
        .expect(200)
        .expect('It Works!'));

Maintainer

Ian Remmel

Contribute

PRs Welcome

License

MIT Β© Ian Remmel 2019 until at least now

tracks-boot's People

Contributors

dependabot-preview[bot] avatar ianwremmel avatar dependabot-support avatar dependabot[bot] avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

lgtm-migrator

tracks-boot's Issues

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


CircleCI using 'master' not configured publish branch (undefined)

Unfortunately this error doesn't have any additional information. Feel free to kindly ask the author of the condition-circle plugin to add more helpful information.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

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.