Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Create a plugin

Burnett edited this page May 14, 2016 · 6 revisions

Plugins

A plugin is a sub-feature and can be easily created.

Plugins are not a part of the core. If you wish to extend and provide a new core-addon to Sys-API, please check the following link: https://github.com/Burnett01/sys-api/wiki/Create-an-Addon-(core)


Create a plugin

#1

Create a new plugin-root-folder at some place. (Example /home/myplugins/)

This folder holds all your plugins!


#2

Now create a folder for your new plugin. (Example: /home/myplugins/MyPlugin

Please note: The given name represents your addon-name!


#3

Create an index.coffee file in your folder.


#4

You can start coding now. The following template might help:

module.exports = {
    namespace:
        hello: (router) ->
            router.send("world")
}        

The namespace should be lower-case and match your plugin-folder-name.

Note: The namespace will be exposed to the scope of the API!

This is how a plugin may look like:

module.exports = {
    myplugin:
        time: (router) ->
            router.send(new Date().getHours())
}        

#5 In order to load your plugins, please change the options of your API-instance.

API = require 'sys-api'

api = new API({
    'plugins.root' : '/home/myplugins/'  #The plugin-root-folder (see #1)
    'plugins.autoload' : true,           #must be true in order to load your plugins
    'restify' : {                        #additional restify settings
    }
})

#6

Your plugin was successfully loaded and you can use it:

api.myplugin.time() #=> 5 

# or in a route:

#Either this way...

api.get('/myplugin/time', api.myplugin.time)


#or that way...

api.get('/myplugin/time', (router) ->
    router.send(api.myplugin.time(router))
)

#or that way...

api.get('/myplugin/time', (router) ->
    api.myplugin.time(router)
)

#or that way...

api.get('/myplugin/time', (router) ->
    router.res.send(api.myplugin.time(router))
    router.next()
)

# etc...

If you use your plugin in a route (aka. as middleware), the first parameter is always the `router-object!

Addons

Clone this wiki locally