@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
}).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
});