@the-/server
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
- InfoFlusher~infoFlusher
- global
- module:@the-/server
- module:@the-/server.adapters
- module:@the-/server.assert
- module:@the-/server.connectors
- module:@the-/server.constants
- module:@the-/server.helpers
- .asStrictSession(session)
- .callbacksProxy(options=)
- .ClientAccess(config)
- .ctxInjector(creators)
- .InfoFlusher(filename,getter)
- .langDetector(locales,options)
- .MetricsCounter()
- .queryFromUrl(urlString)
- .RPCKeeper(Class)
- .serverRendering(Component,options)
- .serverRendering(Html,options=)
- .streamPool()
- .toControllerDriverFactory(ControllerFactory,options=)
- .toLowerKeys(values=)
- ~StreamDriverPool()
- module:@the-/server.helpers.ClientAccess
- module:@the-/server.helpers.InfoFlusher
- module:@the-/server.helpers.metricsMix
- module:@the-/server.helpers.RPCKeeper
- module:@the-/server.stores
- module:@the-/server.streaming
- SessionAccess~sessionAccess
- Store
- TheServer
See API Guide for more detail
License
This software is released under the MIT License.