0.6.0 • Published 8 years ago
soular v0.6.0
Soular :sunny: :earth_americas: :crescent_moon:
What is it?
- Web server library
- Based on promises and a single state atom to handle concurrency
Why use it?
- Small, easy to use (<100 LOC)
- 100% test coverage
- Service Discovery
- React Router integration
How does it work?
Promise.all(middleware).then(mapResultToHttpResponse).catch(ErrorHandler)
All middleware is run at the same time. Unused middleware should short circuit.
Running middleware relies on the ctx.state
variable to communicate with other
middleware.
Basic Example
import soular from 'soular'
const getDBMiddleware = async ctx => {
let db = await DBClient.getConnention()
ctx.state.db = db
}
const handler = async ctx => {
const db = await ctx.state.db
const users = await db.getAllUsers()
return {
body: { users }
}
}
soular('*')
.use(getDBMiddleware)
.use(handler)
.listen()
Routing
import soular from 'soular'
import { GET } from 'soular/route'
const getDB = async ctx => {
const db = await DBClient.getConnention()
ctx.state.db = db
}
const getUsers = async ctx => {
const db = await ctx.state = db
const users = await db.getAllUsers()
return {
body: { users }
}
}
const getAdmins = async ctx => {
const db = await ctx.state.db
const admins = await db.getAdmins()
return {
body: { admins }
}
}
soular('*')
.use(getDB)
.use(GET('/users')(getUsers))
.use(GET('/admins')(getAdmins))
.listen()