Servez Library
raw JSON →Servez-lib, currently at version 2.11.0, is a minimalist HTTP/HTTPS server library primarily designed for internal use within the author's related projects, `servez-cli` and the `servez` web application. It provides basic static file serving capabilities with configurable ports and root directories, including support for index files and logging. The library is explicitly not intended for external consumption or extension by third-party developers; its maintainer strongly recommends copying its source code directly into projects rather than establishing a package dependency. While it offers a simple way to create a local development server, its release cadence and API stability are not managed with external users in mind, making it an unsuitable choice for general-purpose library development. It offers core functionalities like serving index files and basic request handling through a straightforward API.
Common errors
error Error: listen EADDRINUSE: address already in use :::8080 ↓
port option in your createServez configuration to an unused port (e.g., 3000, 8000), or identify and terminate the conflicting process. error Error: ENOENT: no such file or directory, stat '/path/to/nonexistent/root' ↓
root option exists. Create it if necessary before starting the server, or correct the path if it's misspelled. error When accessing the server, I see a directory listing instead of my index.html page. ↓
index option in createServez to the name of your desired index file (e.g., index: 'index.html') and verify that this file exists directly within the root directory you are serving. Warnings
breaking The author explicitly states this library is not intended for external use or extension by third parties; instead, it's recommended to copy the source code directly into your project. Treating it as a general-purpose library will likely lead to unexpected breaking changes, lack of support, or unaddressed issues. ↓
gotcha API stability is not guaranteed for external consumers. Changes may be introduced without strict adherence to semantic versioning principles for third-party integrations, as the library primarily serves internal projects by the same author. ↓
gotcha The library is primarily designed for CommonJS (CJS) environments. While it can be imported in ESM projects using default import syntax, direct named ESM imports or explicit `package.json` `type: "module"` configurations might not be fully supported or tested for all functionalities. ↓
Install
npm install servez-lib yarn add servez-lib pnpm add servez-lib Imports
- createServez wrong
import { createServez } from 'servez-lib';correctconst createServez = require('servez-lib'); - createServez (ESM import) wrong
const createServez = require('servez-lib'); // In an ESM module filecorrectimport createServez from 'servez-lib'; - Types
// No dedicated TypeScript types are officially published or maintained for external use.
Quickstart
const createServez = require('servez-lib');
const path = require('path');
const fs = require('fs');
const port = process.env.PORT ?? 8080;
const rootDir = path.join(__dirname, 'public');
const indexFile = 'index.html';
// Ensure the 'public' directory exists and contains an index.html for demonstration
if (!fs.existsSync(rootDir)) {
fs.mkdirSync(rootDir, { recursive: true });
}
if (!fs.existsSync(path.join(rootDir, indexFile))) {
fs.writeFileSync(path.join(rootDir, indexFile), `<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0">\n <title>Servez Quickstart</title>\n</head>\n<body>\n <h1>Hello from servez-lib!</h1>\n <p>This page is served from the '${path.basename(rootDir)}' directory.</p>\n <p>Server running on port ${port}.</p>\n</body>\n</html>`);
}
async function startServer() {
try {
const serverOptions = {
port: port,
root: rootDir,
index: indexFile,
log: true
};
const server = await createServez(serverOptions);
console.log(`Servez server started successfully.`);
console.log(`Serving files from: ${server.root}`);
console.log(`Access at: http://localhost:${server.port}`);
process.on('SIGINT', async () => {
console.log('\nShutting down Servez server...');
await server.kill();
console.log('Servez server shut down.');
process.exit(0);
});
} catch (error) {
console.error('Failed to start Servez server:', error);
process.exit(1);
}
}
startServer();