Locally invoke any server handler.
✅ Support Web and Node.js compatible handlers.
✅ Does not require a listening server.
✅ Auto detects module based on export signature.
✅ Loader with auto spy to support server entries that are directly listening server.
✅ Zero dependencies.
Important
This is an experimental idea!
Install the package:
# ✨ Auto-detect (supports npm, yarn, pnpm, deno, and bun)
npx nypm install servokeImport:
ESM (Node.js, Bun, Deno)
import {
toWebHandler,
nodeToWebHandler,
invokeWebHandler,
invokeModule,
loadAsWebHandler,
} from "servoke";loadAsWebHandler is the main utility from this package. It:
- Initiates a spy on
node:http:Server.listen - Loads module using dynamic
import() - If no
listencall is detected, tries to detect module exports usingtoWebHandler(if exports are not fetch-compatible, will be converted usingnodeToWebHandler)
You can then directly call the loaded web handler (Request => Promise<Response>) or use invokeWebHandler for more convenience.
Example:
import Express from "express";
const app = Express().use("/", (req, res) => {
res.json({ url: req.url });
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});import { loadAsWebHandler, invokeWebHandler } from "servoke";
const webHandler = await loadAsWebHandler(
new URL("_node-server.mjs", import.meta.url),
);
const res = await invokeWebHandler(webHandler, "/test");
console.log(await res.json()); // { url: '/test' }Convert a Node handler ((req, res) => {...}) to a fetch-compatible Web handler ((Request) => Promise<Response>).
Automatically convert imported module with unknown exports (Node.js or Web syntax) to a fetch-compatible Web handler ((Request) => Promise<Response>).
Throws an error if no compatible handler is found.
local development
Published under the MIT license 💛.