Skip to content

Creating Methods

Mathias Quintero edited this page Mar 18, 2016 · 1 revision

Methods

Methods are the key to your REST API! You will have to create a Method Object in it's own file for every action available in your API.

The key is understanding the file structure and how to create your objects.

The File Structure

In your methods folder you will have a subfolder for every type of action. (GET, POST, PUT, DELETE) Inside those folders will be a file for every url you are responding to. It's important to note that the file names will be used as their urls and they are case sensitive

For instance if your API only listens for a GET at "/hello": You have to create a file "hello.js" inside "/methods/get".

Creating The Method

The big question is what should the file look like. Take this simple HelloWorld example:

var Method = require('Aeolus').Method;

var HelloWorld = new Method();
HelloWorld.handle(function(request, response) {
  response.respondPlainText("Hello World!");
});

module.exports = HelloWorld;

Options

Authentification

Asking For Authentication

If your method requires authentication simply call the .setHasAuth function. For Example with the HelloWorld Method:

HelloWorld.setHasAuth(true);

This makes it so that if the Method get's called, the global Authentication Handler will be called first. If it doesn't clear up, the method won't even be called.

Special Authentication Handler

Sometimes you need different Authentication handlers for different Methods. No problem! You can add special handler that will only be called with that method by calling the function .auth. For example if the HelloWorld method only works for the user "root":

HelloWorld.auth(function(name, pass, callback) {
  callback(name === "root" && pass === "alpine");
});