@speedup/async-http-server
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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

Package Sidebar

Install

npm i @speedup/async-http-server

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

12.1 kB

Total Files

9

Last publish

Collaborators

  • dmanavi