Dolphin Server Modules
Dolphin Server Modules is the core utility package for the Dolphin Framework, an ultra-lightweight and modular backend ecosystem built on native Node.js. It provides fundamental functionalities for building Web, Microservices, and Industrial IoT applications, focusing on Auth, CRUD, Models, and Controllers. The current stable version is 2.2.4, with a recent feature release cadence indicated by updates like v2.2.1 and v2.2 bringing CLI usage and dynamic API proxies. Key differentiators include its zero-dependency core built on native `http` and `events`, universal compatibility with tools like Mongoose and Zod, support for Express-compatible multi-handler middleware, and native IIoT support via binary plugins for protocols like HL7 and Modbus. It also offers a unique server-served client library, eliminating the need for client-side NPM installations.
Common errors
-
ERR_MODULE_NOT_FOUND: Cannot find package 'dolphin-server-modules/server' imported from ...
cause Attempting to use CommonJS `require()` or running an ESM file without proper Node.js ESM configuration.fixEnsure your `package.json` contains `"type": "module"` and all imports use `import ... from '...'`. If using TypeScript, check `tsconfig.json` for `"module": "NodeNext"` or `"ESNext"` and `"moduleResolution": "NodeNext"`. -
TypeError: app.get is not a function
cause Incorrectly importing or initializing the server. This could also happen if `createDolphinServer` is not a named export or if an older version is used.fixVerify that `import { createDolphinServer } from 'dolphin-server-modules/server';` is used, and that the `app` variable is correctly assigned the result of `createDolphinServer()`. -
Error: Middleware must be a function with (ctx, next) signature.
cause Providing a middleware function that does not match Dolphin's expected `(ctx, next)` signature, possibly from an Express.js-centric background.fixRefactor your middleware to accept `ctx` as the first argument and `next` as the second. Use `await next()` to pass control to the next middleware or handler in the chain. -
ReferenceError: dolphin is not defined
cause Attempting to use the `dolphin` client-side global object without including the server-served client library in the HTML.fixEnsure your HTML includes `<script src="/dolphin-client.js"></script>` before any script that attempts to use the `dolphin` global object. The Dolphin server must also be running to serve this file.
Warnings
- breaking Dolphin Framework is ESM-first since early v2.x versions. Using CommonJS `require()` statements for module imports will likely fail or lead to unexpected behavior.
- gotcha The Dolphin client-side library (`dolphin-client.js`) is served directly by the Dolphin server. Do not attempt to install it via npm or other package managers, as it is not distributed that way.
- gotcha The framework primarily uses a unified `ctx` (context) object in route handlers for consistency. While it supports Express-compatible middleware, custom middleware or legacy Express middleware might require adaptation to the `(ctx, next)` signature.
- gotcha The `npx dolphin-server` CLI tool for instantly running a Dolphin server was introduced in v2.2.1. Older versions will not have this command available.
- gotcha The primary official master guide for Dolphin Framework is currently available in Nepali. While code examples are universal, detailed textual explanations are in Nepali, which might be a barrier for non-Nepali speaking developers.
Install
-
npm install dolphin-server-modules -
yarn add dolphin-server-modules -
pnpm add dolphin-server-modules
Imports
- createDolphinServer
const { createDolphinServer } = require('dolphin-server-modules/server');import { createDolphinServer } from 'dolphin-server-modules/server'; - DolphinAuth
import DolphinAuth from 'dolphin-server-modules/auth';
import { DolphinAuth } from 'dolphin-server-modules/auth'; - DolphinCRUD
require('dolphin-server-modules/crud');import { DolphinCRUD } from 'dolphin-server-modules/crud'; - ctx
(req, res) => { /* ... */ }(ctx) => { /* ... */ }
Quickstart
import { createDolphinServer } from 'dolphin-server-modules/server';
const app = createDolphinServer();
app.get('/ping', (ctx) => {
return { message: 'pong', version: '1.5.6' };
});
app.get('/hello/:name', (ctx) => {
const name = ctx.params.name;
return { greeting: `Hello, ${name}!` };
});
app.post('/data', async (ctx) => {
const body = await ctx.req.json(); // Access raw request body
console.log('Received data:', body);
return { status: 'received', data: body };
});
app.listen(3000, () => console.log('🐬 Dolphin swimming on port 3000'));