4.2.0 • Published 2 years ago

@ionaru/web-server v4.2.0

Weekly downloads
4
License
MIT
Repository
github
Last release
2 years ago

@ionaru/web-server

Description

A package for creating a simple web server.

Usage

npm install @ionaru/web-server

Standard http

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

Express

const expressApplication = express();

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

new WebServer(expressApplication, 3000).listen();

Separate listening call

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

webServer.server.on('listening', () => {/* Custom on-listening code */});
webServer.listen();

API

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.infoLogEvent

Info-level log event

// Full-size
const myServer = new WebServer((request, response) => {}, 3000);
myServer.infoLogEvent.on((message) => {
    console.log(message);
});

// Shorthand
const myServer = new WebServer((request, response) => {}, 3000);
myServer.infoLogEvent.on(console.log);

WebServer.prototype.errorLogEvent

Error-level log event This package exposes log levels as events so you can pipe them to your favourite log handler

// Full-size
const myServer = new WebServer((request, response) => {}, 3000);
myServer.errorLogEvent.on((message) => {
    console.error(message);
});

// Shorthand
const myServer = new WebServer((request, response) => {}, 3000);
myServer.errorLogEvent.on(console.error);

WebServer.prototype.listen()

Start listening on the created web server.

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

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
});
4.2.1-15.0

2 years ago

4.2.1-16.0

2 years ago

4.2.1-4.0

3 years ago

4.2.1-6.0

3 years ago

4.1.1-41.0

3 years ago

4.1.2-3.0

3 years ago

4.1.0

3 years ago

4.2.0

3 years ago

4.1.1

3 years ago

4.0.1-32.0

4 years ago

4.0.1-31.0

4 years ago

4.0.0

4 years ago

3.0.1-26.0

4 years ago

4.0.1-27.0

4 years ago

3.0.1-23.0

5 years ago

3.0.0

5 years ago

3.0.1-21.0

5 years ago

2.0.0

5 years ago

2.0.1-17.0

5 years ago

1.0.2

5 years ago

1.0.3-16.0

5 years ago

1.0.2-10.0

5 years ago

1.0.2-9.0

5 years ago

1.0.1

5 years ago

1.0.2-7.0

5 years ago

1.0.1-6.0

5 years ago

1.0.0

5 years ago

1.0.1-4.0

5 years ago

0.0.3-3.0

5 years ago

0.0.3-2.0

5 years ago

0.0.3-1.0

5 years ago

0.0.1

5 years ago