Skip to content

Latest commit

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Module API Server

Instantly expose server-side node modules through a REST API.

// server sidemodule.exports={hello: name=>`hello ${name}`};
npx module-api-server ./my-module.js
> Loaded namespace { hello: [Function: hello] }
> Serving on port 1234, endpoint: /api, CORS enabled
// client sideconstclient=require('module-api-client');constapi=client();constresult=awaitapi.hello('andrei');// => hello andrei

That's it.

How it works

On the server-side, the library loads your modules into a unified namespace and exposes it through a simple REST API.

Start a standalone server through the npx CLI:

npx module-api-server -p 1234 --cors --endpoint=/api my-modules/hello my-modules/time
Loaded namespace { time: [Function], hello: [Function: hello] }
Serving on port 1234, endpoint: /api, CORS enabled

Or you can attach as middleware to your existing express server:

npm install module-api-server
constexpress=require('express');constapp=express();const{ middleware }=require('module-api-server').server({paths: [...],// array of module absolute pathsmodules: {...},// map of preloaded modulesendpoint: '/api',// optional, defaults to /cors: false,// optional, defaults to false});app.use(middleware);

The client library first fetches the namespace through the API and mirrors it as local async functions (promises). When a local function is executed, the library calls into its remote counterpart through the API and returns the result value.

importclientfrom'module-api-client';constapi=client({endpoint: '/api',// optional, defaults to /});// POST /api/hello ["andrei"] => "hello andrei"constresult=awaitapi.hello('andrei');

Author

Andrei Gheorghe

About

Instantly expose server-side node modules as REST API and easily consume through mirrored client-side functions.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages