server-base is a foundational package designed for quickly setting up HTTP microservices or simple web servers. Currently at version 7.1.32, it provides core functionalities such as declarative request routing, middleware integration, and automatic `.env` file loading for configuration management. It leverages `server-base-router` for its routing engine and integrates structured logging capabilities via `server-base-log`, which itself builds upon `pino` for high-performance logging. Additionally, it uses `fast-json-stringify` for optimized JSON serialization. While the README doesn't specify a precise release cadence, its active development and frequent version updates suggest ongoing maintenance. Its key differentiator lies in its opinionated, modular approach to server development, focusing on simplicity, performance, and testability for small to medium-sized services, abstracting away common boilerplate found in more comprehensive frameworks.
npm install server-baseVerified import paths — ran on the pinned version, not inferred.
Initializes a `server-base` instance, setting up global middleware in the `@setup` hook and defining two API endpoints: a `/graphql` path with GET and async POST handlers (demonstrating JSON parsing), and a `/health` endpoint.
For ESM projects, try `import service from 'server-base';` and ensure Node.js is configured to handle CJS modules in an ESM context (e.g., by using an ESM wrapper or a build tool). If direct import fails, consider transpilation or using a CommonJS project setup.
Avoid initializing `dotenv` manually if `server-base` is used, or ensure explicit order of operations if custom environment loading is critical. Test thoroughly to prevent unintended configuration overrides.
Always pass an array of middleware functions to `ctx.use()`, even if it's just a single function (e.g., `ctx.use([myMiddleware])`).
Access the logger instance (if exposed via context or options) and use its methods (e.g., `ctx.log.info('message')`, `ctx.log.error('error')`) for all application logging to ensure consistency and proper structured output.Ensure you are using `const service = require('server-base')` for CommonJS projects or `import service from 'server-base'` if your environment correctly handles ES Modules and the package exports a default. Double-check `package.json` for `"type": "module"` if using ESM.Change the port number in your `.start()` call (e.g., `.start(3001)`) or ensure no other applications are using the desired port. On Linux/macOS, `lsof -i :PORT_NUMBER` can identify the process, and `kill -9 PID` can terminate it.