@the-/server

HTTP/RPC Server of the-framework

Usage no npm install needed!

<script type="module">
  import theServer from 'https://cdn.skypack.dev/@the-/server';
</script>

README

@the-/server

npm Version

HTTP/RPC Server of the-framework

Installation

$ npm install @the-/server --save

Usage

'use strict'

const React = require('react')
const theServer = require('@the-/server')

const { createElement: c } = React

;(async () => {
  // Define RPC Controller Class
  const FruitShopCtrl = ({ session }) => ({
    async addToCart(name, amount = 1) {
      const { cart = {} } = session
      cart[name] = (cart[name] || 0) + amount
      session.cart = cart
    },
    async buy() {
      const { cart = {} } = session
      console.log(cart)
      /* ... */
    },
  })

  // Define real time event handling stream
  const CountdownStream = ({ params }) => ({
    async *provide() {
      let count = params.count || 100
      while (count > 0) {
        yield { count }
        count--
      }
    },
  })

  const server = theServer({
    // Register controller with name
    // Controller instance will be created for each method call
    controllers: {
      fruitShop: FruitShopCtrl,
    },
    /**
     * View renderer
     * @param {*} children
     */
    html: ({ children }) => c('html', {}, c('body', {}, children)),
    /**
     * Redis config
     */
    redis: { db: 1, host: '127.0.0.1', port: '6379' },
    /**
     * Directory path to serve static files
     */
    static: 'public',
    streams: {
      countdown: CountdownStream,
    },
  })

  await server.listen(3000)
})().catch((e) => console.error(e))

API Guide

See API Guide for more detail

License

This software is released under the MIT License.

Links