Handling HTTP Request for Kites
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.editorconfig
.gitignore
.travis.yml
LICENSE
README.md
index.js
kites.config.js
package.json

README.md

kites-express

Handling HTTP request for Kites

Join the chat at https://gitter.im/nodevn/kites npm version npm downloads Travis

Features

  • Add express req/res method utilities:
    • req.param('paramName', 'defaultValue')
    • res.ok(data)
    • res.nok(err)
    • res.error(err)
    • res.badRequest(err)
    • res.serverError(err)
    • res.forbidden(err)
  • Rendering template using with express response helpers:
    • res.view('path/to/template', data, callback)
  • Optional to enable static public directory, see extension options configuration
    • express.static: false (default)
    • express.static: "path/to/public/directory"

Options

  • static: Public directory contains static files to serve client, default: false
  • httpPort: Port which express listens to, default: 8000
  • poweredBy: Header X-Powered-By will response to client, default: Kites
  • views.ext: View engine extension, default: ejs
  • views.engine: View engine package which can be required, default: ejs
  • views.renderer: Options describe how kites knows to initialize, default: renderFile
  • views.path: Path contains view template, default: <root-project-location>/views
  • views.locals: Default data send to view, default: false

Usage

You can apply this extention manually to kites-engine

var kites = require('@kites/engine')()
kites.use(require('@kites/express')())

Auto discover mode, just install the extension as a dependency:

npm install @kites/express

Example:

Configure view engine: Pug

{
    "views": {
        "ext": "pug",
        "engine": "consolidate"
    }
}

Configure view engine: Handlebars

{
    "views": {
        "ext": "hbs",
        "engine": "hbs",
        "renderer": "__express",
        "locals": false,
        "path": "./views"
    }
}

Configure view engine: Dust.js by LinkedIn

{
    "views": {
        "engine": "adaro",
        "renderer": {
            "method": "dust",
            "arguments": [{
                "cache": false,
                "helpers": ["dust-helpers-whatevermodule"]
            }]
        }
    }
}

API

Request:

These helpers are utilities help you access kites and request information easily.

  • req.kites - Access kites from http request
  • req.wantsJSON - Flag indicating whether a request would like to receive a JSON response
  • req.param(name: String, default: Any) - Get param from user request

Ex:

info (req, res) => {
    req.kites.logger.info('Access kites from Request!');

    return res.ok({
        greeting: 'Hello World!'
    });
}

Response:

These helpers are utilities help you handling response and generating template.

  • res.view() - an enhanced version of res.render
  • res.ok(data) - Return data for client, auto detect JSON needed from request.
  • res.nok(err) - Return error message for client, auto detect error, 400 default.
  • res.error(err) - Alias: res.nok()
  • res.badRequest(err) - Bad request
  • res.notFound(err) - Not found
  • res.forbidden(err) - Forbidden
  • res.serverError(err) - Server error

Event:

Some events can easily override to make program has the right behaviour.

  • expressConfigure:view

Notice

  • Be sure either enable mode auto discover or use extensions programatically and respectively. If not, you may get message: Error: listen EADDRINUSE :::8000

License

MIT License

Copyright (c) 2018 Nhữ Bảo Vũ

The MIT License