17.7.10 • Published 5 years ago

@the-/server v17.7.10

Weekly downloads
554
License
MIT
Repository
github
Last release
5 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

5 years ago

17.7.9

5 years ago

17.7.7

5 years ago

17.7.6

5 years ago

17.7.5

5 years ago

17.7.4

5 years ago

17.7.0

5 years ago

17.6.4

5 years ago

17.6.3

5 years ago

17.6.2

5 years ago

17.4.0

5 years ago

17.6.0

5 years ago

17.3.1

5 years ago

17.3.0

5 years ago

17.2.1

5 years ago

17.0.1

5 years ago

16.3.7

5 years ago

16.3.5

6 years ago

16.3.4

6 years ago

16.3.3

6 years ago

16.3.1

6 years ago

16.2.1

6 years ago

16.2.0

6 years ago

16.1.2

6 years ago

16.1.1

6 years ago

16.1.0

6 years ago

16.0.34

6 years ago

16.0.33

6 years ago

16.0.32

6 years ago

16.0.30

6 years ago

16.0.29

6 years ago

16.0.26

6 years ago

16.0.24

6 years ago

16.0.20

6 years ago

16.0.18

6 years ago

16.0.10

6 years ago

16.0.8

6 years ago

16.0.7

6 years ago

16.0.6

6 years ago

16.0.3

6 years ago

15.4.11

6 years ago

15.4.10

6 years ago

15.4.9

6 years ago

15.4.2

6 years ago

15.4.1

6 years ago

15.2.5

6 years ago

15.2.4

6 years ago

15.2.3

6 years ago

15.2.1

6 years ago

15.2.0

6 years ago

15.1.4

6 years ago

15.1.2

6 years ago

15.1.0

6 years ago

15.0.3

6 years ago

15.0.2

6 years ago