Coder Social home page Coder Social logo

cranny's Introduction

cranny

A Node.js server architecture with a focus on isolation, segmentation, and safe request handling.

Base Endpoints

endpoint.js is the base of each endpoint. It handles the sending of the response including any errors which were thrown.

Endpoint functionality is constructed through higher-order functions.

rest.js is an example of a higher-order function derived from endpoint.js that handles specific RESTful aspects of the corresponding call.

Endpoint creation

The files which represent endpoints should either export an array which may look like:

const {rest} = require('cranny');
module.exports = [
  'post', // type
  rest(async (req, res, signal) => {
    const someFunction = require('../someFunction');
    return await someFunction(); // Returns an object or null.
  })
];

or just a function like this:

const {rest} = require('cranny');
module.exports = rest(async (req, res, signal) => {
  const someFunction = require('../someFunction');
  return await someFunction(req); // Returns an object or null.
});

Hosting endpoints

After initializing your server (let's say an express server), call discoverEndpoints to collect all hostable endpoints from a given root. You may then host the endpoints like this:

const express = require('express');
const {discoverEndpoints, rest} = require('cranny');

const app = express();
app.use(express.raw({ type: 'application/json', limit: '10mb' }));
app.use(cors());

const endpoints = discoverEndpoints(__dirname);

for (const endpoint of endpoints) {
    const type = endpoint.type;
    const name = endpoint.name;
    const func = endpoint.obj;
    app[type](`/${name}`, func);
}

or for Firebase cloud functions for example:

const functions = require('firebase-functions');
const {discoverEndpoints, rest} = require('cranny');

const restEndpoints = discoverEndpoints(__dirname);

for (const endpoint of restEndpoints) {
    const name = endpoint.name;
    const func = endpoint.obj;
    exports[name] = functions.region('us-central1').https.onRequest(async (req, res) => {
      cors(req, res, async () => {
        await rest(func(req, res));
      });
    });
}

Each endpoint has 3 properties:

type: The type of the endpoint (like 'get' or 'post'). May be excluded/null.

name: The name of the endpoint

obj: The obj representing the required file. Often times is a function with the req, res, and optional signal parameters.

Signal parameter

You endpoints can take advantage of the abort signal which is passed in next to req, and res. The abort signal will fire when the user ends the connection prematurely. It can act as an interrupt.

Naming Convention

The discoverEndpoints call looks for files which have a special suffix .rest.js.

You may also pass in your own suffixes to the call to get different endpoints.

Every file which you want to represent as an endpoint should have a unique name.

Benefits of architecture

Don't ever need to handle the sending of responses yourself.

Don't need to handle the sending of error responses in case of error.

No monotonically increasing index.js file.

Endpoints can be created in isolation and never need to touch core code.

Higher-order functions abstract away most of the network layer.

Foolproof Error Handling

Wherever you setup your environment, you can set this global variable:

global.crannyReportError = (e) => { ... }

This is the global fallback that ensures that any unexpected error in the request is handled/reported properly.

Publish

Published with npm publish

cranny's People

Contributors

wtesler avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.