-
Notifications
You must be signed in to change notification settings - Fork 1
Create a plugin
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 new plugin-root-folder at some place. (Example /home/myplugins/)
This folder holds all your plugins!
Now create a folder for your new plugin. (Example: /home/myplugins/MyPlugin
Please note: The given name represents your addon-name!
Create an index.coffee
file in your folder.
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())
}
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
}
})
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