-
Notifications
You must be signed in to change notification settings - Fork 2
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.
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
};
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.
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.
Use ssl-new
command to generate a new, self-signed certificate.
licensed under GDFL 1.3 - © e.GO Digital GmbH