2.0.2 • Published 6 years ago

sg-server v2.0.2

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
6 years ago

sg-server

Build Status npm Version JS Standard

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

ArgTypeDefaultDescription
configobjectServer configuration
config.middlewaresfunction[][]Middlewares instance
config.keysstringKoa keys
config.contextObjectKoa context prototype
config.onErrorfunctionError handler
config.staticstringPublic directories.
config.endpointsObjectEndpoint settins

License

This software is released under the Apache-2.0 License.

Links