eliter
Tiny web server
Last updated 2 years ago by hcyue .
MIT · Repository · Bugs · Original npm · Tarball
$ cnpm install eliter 
SYNC missed versions from official npm registry.

Eliter

Build Status Commitizen friendly

Eliter is a micro web server based on Node.js, supporting route, session and static file, using co-like generator-based APIs.

Quick Start

Install

npm install eliter --save

Hello World


const Eliter = require('eliter')

let app = new Eliter()

app.route('/').get(function* () {
    this.send('text', 'hello eliter!') // `this` there is an instance of class Connection
})

// `::` stands for a URL param
app.route('/do/::', function* (act, child) {
    this.act = act
    yield* child // the last param is handler of child route
}).route('/::').get(function* (name) {
    this.send('text', `${this.act} ${name}`)
})

// /do/hello/world => "hello world"

app.start(4000)

What Is It Like

Generator-based Async

const app = new Eliter()
app.route('/').get(function *() {
    const data = yield getFromModel()
    this.send('json', data)
})

Nested Router and Middleware

const app = new Eliter()

const admin = app.route('/admin', function *(child) {
    const { data: { auth }} = yield this.session()
    if (auth) {
        yield* child
    } else {
        this.send({ status: 302, headers: { Location: '/login' }})
    }
})

const profile = admin.route('/profile').get(function* () {
    const data = yield getFromModel()
    this.send('html', `<p>Hello, ${data.username}<p>`)
})

Pluginable

const app = new Eliter()

const checkAuth = (conn) => conn.checkAuth = function* () {
    const { data: { auth }} = yield this.session()
    return !!auth
}

app.with(checkAuth)

const admin = app.route('/admin', function *(child) {
    if (yield* this.checkAuth()) {
        yield* child
    } else {
        this.send({ status: 302, headers: { Location: '/login' }})
    }
})

Docs - FIXME

Eliter and Connection docs

Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (4)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |