Skip to content
This repository has been archived by the owner on Jun 27, 2021. It is now read-only.
Marcel Kloubert edited this page Aug 17, 2019 · 4 revisions

APIs

api command allows you to run a REST API, using Express.js framework, from current directory.

To start, create an index.js file with the following skeleton:

exports.GET = async function(req, res) {
    // s. https://egodigital.github.io/ego-cli/interfaces/_contracts_.commandexecutecontext.html
    const CONTEXT = this;

    return res.status(200)
        .send('Hello, e.GO!');
};

Run a host instance, by executing

ego api

from that directory and open http://localhost:8080/api/ from browser to see the result of the endpoint.

Other methods

To use other HTTP methods, like POST, PUT or DELETE, simply export functions with their names, in upper case characters:

exports.POST = async function(req, res) {
    // TODO: implement
};

exports.PUT = async function(req, res) {
    // TODO: implement
};

exports.PATCH = async function(req, res) {
    // TODO: implement
};

exports.DELETE = async function(req, res) {
    // TODO: implement
};

To handle any method, you only need to implement a request function:

exports.request = async function(req, res) {
    // TODO: implement
};

Routes

If you would like to implement an /foo/bar endpoint e.g., you have to create one of the following files

  • /foo/bar.js
  • or /foo/bar/index.js

Files with leading _ will be ignored.

Bootstrap and shutdown scripts

To define startup logic, create a _bootstrap.js at the root of your API directory:

exports.execute = async (context, apiRouter, app) => {
    // context    =>  https://egodigital.github.io/ego-cli/interfaces/_contracts_.commandexecutecontext.html
    // apiRouter  =>  https://expressjs.com/en/guide/routing.html
    // app        =>  https://expressjs.com/en/starter/hello-world.html
};

You can do a similar thing with a _shutdown.js file.

SSL

Use ssl-new command to generate a new, self-signed certificate.

Clone this wiki locally