sg-server

2.0.2 • Public • Published

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

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.

Links

/sg-server/

    Package Sidebar

    Install

    npm i sg-server

    Weekly Downloads

    6

    Version

    2.0.2

    License

    Apache-2.0

    Unpacked Size

    22.5 kB

    Total Files

    14

    Last publish

    Collaborators

    • realglobe