0.1.10 • Published 4 years ago

module-api-server v0.1.10

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

Module API Server

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

// server side

module.exports = { hello: name => `hello ${name}` };
// client side

const client = require('module-api-client');
const api = client();
const result = await api.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
const express = require('express');
const app = express();

const { middleware } = require('module-api-server').server({
  paths: [...], // array of module absolute paths
  modules: {...}, // map of preloaded modules
  endpoint: '/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.

import client from 'module-api-client';

const api = client({
  endpoint: '/api', // optional, defaults to /
});

// POST /api/hello ["andrei"] => "hello andrei"
const result = await api.hello('andrei');

Author

Andrei Gheorghe

0.1.10

4 years ago

0.1.8

4 years ago

0.1.9

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.4

4 years ago

0.1.5

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago