Nutella Peanut-Butter Marshmallow

npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript.Learn more »

@kites/express

1.0.1 • Public • Published

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.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: 3000
  • 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

API Usage

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({
    from: 'kites',
    message: '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) - 200 OK. Return data for client, auto detect JSON needed from request.
  • res.error(err) - 40x, 50x Response. Return error message for client, auto detect error, 400 default.
  • res.badRequest(err) - 400 Bad request
  • res.notFound(err) - 404 Not found
  • res.forbidden(err) - 403 Forbidden
  • res.serverError(err) - 500 Server error

Kites Express Events:

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

  • before:express:config - Before express configured
  • express:config - Custom express configuration
  • express:config:view - Before view engine configured, if the event return false, kites express default engine will be used

Example

You can apply this extention manually to kites-engine

import kites from '@kites/engine';
import express from '@kites/express';
 
kites().use(express());

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"]
            }]
        }
    }
}

License

MIT License

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

The MIT License

install

npm i @kites/express

Downloadsweekly downloads

183

version

1.0.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability