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 servoke
Import:
undefinedESM (Node.js, Bun, Deno)
import {
toWebHandler,
nodeToWebHandler,
invokeWebHandler,
invokeModule,
loadAsWebHandler,
} from "servoke";
loadAsWebHandlerloadAsWebHandler is the main utility from this package. It:
node:http:Server.listenimport()listen call is detected, tries to detect module exports using toWebHandler (if exports are not fetch-compatible, will be converted using nodeToWebHandler)You can then directly call the loaded web handler (Request => Promise<Response>) or use invokeWebHandler for more convenience.
undefinedExample:undefined
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' }
nodeToWebHandler(nodeHandler)Convert a Node handler ((req, res) => {...}) to a fetch-compatible Web handler ((Request) => Promise<Response>).
toWebHandler(mod)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.
Published under the MIT license 💛.