node-server-utils
TypeScript icon, indicating that this package has built-in type declarations

0.0.104 • Public • Published

Node Server Utils

Status: Beta

Build Status

Utility functions for creating node server. Handles tracking open sockets and properly shutting down sockets when shutting down the server.

  • Cluster support for running a clustered server. See the node cluster module

Example Typescript

import { Server, STATUS_CODES } from 'http'
import { consoleLogger, createAppServer, logger, shutdownAppServer, startCluster } from '../src'
 
let server: Server
 
async function startApp() {
    server = createAppServer((req, res) => {
        logger.info({ message: STATUS_CODES[200], method: req.method, url: req.url })
        res.writeHead(200, { 'Content-Type': 'text/plain' })
        res.write('OK')
        res.end()
    })
}
 
async function shutdownApp() {
    shutdownAppServer(server)
}
 
async function main() {
    await startCluster(startApp, shutdownApp, consoleLogger)
}
 
void main()

Logger

The logger is setup to push log messages from the cluster workers to the master thread for logging. Register a logger for logging by either passing it into startCluster() or setLogger().

Readme

Keywords

none

Package Sidebar

Install

npm i node-server-utils

Weekly Downloads

45

Version

0.0.104

License

ISC

Unpacked Size

42.8 kB

Total Files

26

Last publish

Collaborators

  • jtalton