Pixl Server Daemon Framework
Pixl-server is a generic server daemon framework for Node.js, designed to provide a foundational layer for building custom server applications such as web backends. It offers essential services like configuration file loading, command-line argument parsing, and robust logging capabilities out-of-box. The core differentiator is its plug-in component system, allowing developers to extend server functionality with pre-built or custom components (e.g., pixl-server-web for HTTP serving, pixl-server-api for JSON APIs). The current stable version is 1.0.50. Based on the provided release history, it appears to follow a slow, incremental release cadence within its 1.x major version, with initial release 1.0.0. This framework enables developers to quickly set up and manage daemon processes with modular, extensible functionality.
Common errors
-
Error: Cannot find module 'pixl-server-web'
cause A required component module has not been installed via npm.fixRun `npm install pixl-server-web` (or the specific missing component) in your project directory. -
Error: listen EADDRINUSE: address already in use :::8080
cause The configured port for the server is already in use by another process.fixChange the `http_port` in your server's configuration to an available port, or stop the conflicting process. For privileged ports (e.g., 80), ensure no other web server is running. -
Server starts but web content is not served or 404s for static files.
cause The `http_htdocs_dir` configuration for `pixl-server-web` is incorrect, or the files are missing/have incorrect permissions.fixVerify that `http_htdocs_dir` points to the correct absolute path where your static files reside, and ensure the Node.js process has read access to that directory and its contents.
Warnings
- gotcha The order of components in the `components` array matters. Components that depend on others (e.g., `pixl-server-api` depends on `pixl-server-web`) must be listed after their dependencies to ensure proper initialization.
- gotcha Configuration can be overridden by multiple sources (config files, command-line arguments, environment variables). Understanding this hierarchy is crucial to avoid unexpected behavior, as settings can be silently overwritten.
- gotcha A basic Pixl-Server instance without any components will not perform any useful tasks. Developers must explicitly `require()` and add components to the `components` array to extend functionality.
- gotcha When using `pixl-server-web` (or any component binding to a port), listening on privileged ports (below 1024, like 80 or 443) typically requires root privileges. Running Node.js processes as root is a security risk.
- breaking Based on the provided release history and documentation, there are no explicitly documented breaking API changes within the 1.x major version of `pixl-server` itself. Breaking changes primarily apply to specific `pixl-server-*` components or their upstream dependencies.
Install
-
npm install pixl-server -
yarn add pixl-server -
pnpm add pixl-server
Imports
- PixlServer
import PixlServer from 'pixl-server';
const PixlServer = require('pixl-server'); - WebServerComponent
import { WebServer } from 'pixl-server-web';const WebServer = require('pixl-server-web'); - APIComponent
import PixlServerAPI from 'pixl-server-api';
require('pixl-server-api')
Quickstart
const PixlServer = require('pixl-server');
const path = require('path');
const fs = require('fs');
// Create dummy directories for demonstration
const logDir = path.join(__dirname, 'log');
const htdocsDir = path.join(__dirname, 'www');
if (!fs.existsSync(logDir)) fs.mkdirSync(logDir, { recursive: true });
if (!fs.existsSync(htdocsDir)) fs.mkdirSync(htdocsDir, { recursive: true });
fs.writeFileSync(path.join(htdocsDir, 'index.html'), '<h1>Hello from Pixl-Server!</h1>');
let server = new PixlServer({
__name: 'MyWebServer',
__version: "1.0.50",
config: {
"log_dir": logDir, // Use local log directory
"debug_level": 9,
"WebServer": {
"http_port": 8080, // Use a non-privileged port
"http_htdocs_dir": htdocsDir, // Use local htdocs directory
"http_ip_address": "127.0.0.1" // Explicitly bind to localhost
}
},
components: [
require('pixl-server-web')
]
});
server.startup( function(err) {
if (err) {
console.error("Server startup failed:", err);
process.exit(1);
}
console.log(`MyWebServer v${server.__version} started on http://127.0.0.1:8080`);
console.log(`Serving static files from: ${htdocsDir}`);
console.log("Press Ctrl+C to stop the server.");
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log("\nShutting down server...");
server.shutdown(() => {
console.log("Server shut down gracefully.");
process.exit(0);
});
});
} );