kirk.js

3.3.1 • Public • Published

kirk.js

This module helps you handling express apps. It loads routers and controllers automatically.

Install

npm install --save kirk.js

Usage

const kirk = require('kirk.js');

kirk.set({ port: 8080 })
.listen()
.then(() => console.info(`started on port ${kirk.config.port}`));

kirk.js loads express routers and controllers automatically and bootstraps an express server. The module relies on actual files and has defaults for the following file structure:

project/
|- controllers/
|- routers/

You can configure the folders using set() (please check the options for that).

set(config:object):object

Sets the main configuration for kirk. You can set the following options:

Field Default Description
port 0 The port of the listed express app. 0 means random port.
controllersDir process.cwd()/controllers The location of the controllers.
routersDir process.cwd()/routers The location of the routers.

listen():Promise

This loads the controllers and routers and start the express app. The Promise resolves if the server is running and listed properly.

close():Promise

This shuts down the server (if it was open), and Resolves when everything is done.

Exposed variables

kirk.js exposes the following variables:

Field Description
kirk.config This holds the main configuration (check config for more info)
kirk.app The actual express app.
kirk.listener The listener, which is undefined until listen() is called. It holds the current listener of the server.
kirk.server The http service by Node.
kirk.Controller A generic class for controllers, which extends EventEmitter.

Debug

This module exposes debug messages using the debug module. Just expose DEBUG=kirk and you will get nice debug messages.

Routers

Routers are simple express Routers and you have to export one router per file:

routers/user.js

const router = require('express').Router();

router.get('/', (req, res) => {});
...

router.prefix = '/api/users'
module.exports = router;

You can define a special prefix attribute on the router to prefix all routes easily.

Controllers

Controllers are simple object, which get loaded (using require). Optionally you can extend kirk's controller object, wich is also an EventEmitter.

const controller = new require('kirk.js').Controller();

controller.handleUser = (user, attribute) => {}

module.exports = controller;

Not sure if this actually works, but you get the idea

Readme

Keywords

none

Package Sidebar

Install

npm i kirk.js

Weekly Downloads

2

Version

3.3.1

License

ISC

Last publish

Collaborators

  • becuz
  • sosedev
  • s.bider
  • manixx