@ionaru/web-server
TypeScript icon, indicating that this package has built-in type declarations

4.2.0 • Public • Published

@ionaru/web-server

npm version npm version Build Status codecov

Description

A package for creating a simple web server.

Usage

npm install @ionaru/web-server

Standard http

new WebServer((request, response) => {
    // Handle request
}).listen();

Express

const expressApplication = express();

expressApplication.use('/', (request, response) => {
    // Handle request
});

new WebServer(expressApplication).listen();

Separate listening call

const webServer = new WebServer((request, response) => {}, 3000);

// Accessed through the exposed Node.js Server instance.
webServer.server.on('listening', () => {/* Custom on-listening code */});
webServer.listen();

Examples

WebServer.prototype.server

The created http.Server instance is exposed in the WebServer instance.

const myServer = new WebServer((request, response) => {}, 3000).listen();
myServer.server.address();

WebServer.prototype.close() (async)

An promisified version of the standard .close(callback)

const myServer = new WebServer((request, response) => {}, 3000).listen();
await myServer.close();

The promise will emit an error when the server was not open when closed.

const myServer = new WebServer((request, response) => {}, 3000).listen();
await myServer.close().catch((error) => {
    // handle error
});

Readme

Keywords

none

Package Sidebar

Install

npm i @ionaru/web-server

Weekly Downloads

1

Version

4.2.0

License

MIT

Unpacked Size

12.2 kB

Total Files

7

Last publish

Collaborators

  • ionaru