Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@speedup/async-http-server

SpeedUp-io23MIT1.0.2TypeScript support: included

Asynchronous wrapper for HTTP server

speedup, http, http-server, async, asynchronous

readme

Asynchronous wrapper for NodeJS HTTP

Instead of using old-fashioned callback functions to initialize an HTTP server, use promises (Async API).

NPM version NPM downloads

Installation


# NPM
npm i @speedup/async-http-server --save

# Yarn
yarn install @speedup/async-http-server

Usage

JavaScript


const AsyncHTTPServer = require('@speedup/async-http-server').default;

const instance = new AsyncHTTPServer({
    port: 3000, // it can be a pipe, too
    handler: (req, res) => {} // Any request-handler compatible function
});

// Legacy way

instance.start({
    port: 4000, // you can override it here
    handler: (req, res) => {} // you can override the handler as well.
})
.then(asyncServer => {

    console.info('HTTP server started!');
})
.catch(err => {

    console.error('Failed to start HTTP server', err);
    process.exit(1);
});

// Modern way

try {
    await instance.start({
        port: 4000, // you can override it here
        handler: (req, res) => {} // you can override the handler as well.
    });
}
catch(err) {

    console.error('Failed to start HTTP server', err);
    process.exit(1);
}

TypeScript


import AsyncHTTPServer from '@speedup/async-http-server';

const instance = new AsyncHTTPServer({
    port: 3000, // it can be a pipe, too
    handler: (req, res) => {} // Any request-handler compatible function
});

// Legacy way

instance.start({
    port: 4000, // you can override it here
    handler: (req, res) => {} // you can override the handler as well.
})
.then(asyncServer => {

    console.info('HTTP server started!');
})
.catch(err => {

    console.error('Failed to start HTTP server', err);
    process.exit(1);
});

// Modern way

try {
    await instance.start({
        port: 4000, // you can override it here
        handler: (req, res) => {} // you can override the handler as well.
    });
}
catch(err) {

    console.error('Failed to start HTTP server', err);
    process.exit(1);
}

And you're good to go!

License

MIT