sg-server
v2.0.2
Published
HTTP server for SUGOS
Downloads
31
Readme
sg-server
HTTP server for SUGOS
Using koa as a base framework
Installation
$ npm install sg-server --save
Usage
'use strict'
const sgServer = require('sg-server')
const co = require('co')
co(function * () {
let server = sgServer({
/** Static directories to serve */
static: [ 'public' ],
/** Koa middlewares to use */
middlewares: [
co.wrap(function * customEndpoint (ctx, next) {
/* ... */
yield next()
})
],
/** Endpoint handlers */
endpoints: {
'/api/foo/:id': { // Pass object to handle each HTTP verbs
'POST': (ctx) => {
let { id } = ctx.params
ctx.body = `This is foo with id: "${id}"`
}
},
'/api': '/api/index', // Pass string to create alias
'/api/index': () => { /* ... */ } // Pass function to handle all HTTP verbs
}
})
yield server.listen(3000)
// To close server.
// yield server.close()
}).catch((err) => console.error(err))
Signature
sgServer(config) -> http.Server
Create web server using koa
| Arg | Type | Default | Description | | --- | ---- | --- | --- | | config | object | | Server configuration | | config.middlewares | function[] | [] | Middlewares instance | | config.keys | string | | Koa keys | | config.context | Object | | Koa context prototype | | config.onError | function | | Error handler | | config.static | string | | Public directories. | | config.endpoints | Object | | Endpoint settins |
License
This software is released under the Apache-2.0 License.