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

Create an Addon (core)

Burnett edited this page May 14, 2016 · 5 revisions

Addons (core)

Create a core-addon

#1

Create a new folder inside /src/addons/ (example: MyAddon).

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


#2

Create an index.coffee file in your folder.


#3

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

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

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

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

This is how an addon may look like:

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

#4

Register your addon to the core-addon container inside API.coffee:

# Define Core-Addons
Addons = [
    "./addons/Fs",
    "./addons/Os",
    "./addons/Net",
    "./addons/MyAddon" # <-- This is your new addon
]

#5

Your addon was successfully registered and you can use it:

api.myaddon.time() #=> 5 

# or in a route:

#Either this way...

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


#or that way...

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

#or that way...

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

#or that way...

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

# etc...

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

Addons

Clone this wiki locally