17.7.10 • Published 3 years ago

@the-/server v17.7.10

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

@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

17.7.10

3 years ago

17.7.9

3 years ago

17.7.7

4 years ago

17.7.6

4 years ago

17.7.5

4 years ago

17.7.4

4 years ago

17.7.0

4 years ago

17.6.4

4 years ago

17.6.3

4 years ago

17.6.2

4 years ago

17.4.0

4 years ago

17.6.0

4 years ago

17.3.1

4 years ago

17.3.0

4 years ago

17.2.1

4 years ago

17.0.1

4 years ago

16.3.7

4 years ago

16.3.5

4 years ago

16.3.4

4 years ago

16.3.3

5 years ago

16.3.1

5 years ago

16.2.1

5 years ago

16.2.0

5 years ago

16.1.2

5 years ago

16.1.1

5 years ago

16.1.0

5 years ago

16.0.34

5 years ago

16.0.33

5 years ago

16.0.32

5 years ago

16.0.30

5 years ago

16.0.29

5 years ago

16.0.26

5 years ago

16.0.24

5 years ago

16.0.20

5 years ago

16.0.18

5 years ago

16.0.10

5 years ago

16.0.8

5 years ago

16.0.7

5 years ago

16.0.6

5 years ago

16.0.3

5 years ago

15.4.11

5 years ago

15.4.10

5 years ago

15.4.9

5 years ago

15.4.2

5 years ago

15.4.1

5 years ago

15.2.5

5 years ago

15.2.4

5 years ago

15.2.3

5 years ago

15.2.1

5 years ago

15.2.0

5 years ago

15.1.4

5 years ago

15.1.2

5 years ago

15.1.0

5 years ago

15.0.3

5 years ago

15.0.2

5 years ago