API Core - Quick Start
This guide will help you learn API Core by creating a simple API using Express as a provider and MongoDB as model backed by the API Core framework.
Preparation and Installation
First install the dependencies and prepare the package:
$ yarn init$ yarn add api-core api-provider-express api-modell-mongoose api-provider$ yarn add express mongoose$ yarn add @types/express @types/mongoose
Also create a
Creating the web server
To provide the to-be-created API, you will need a web server. This could be anything, from a server created using only the built in HTTP library of Node to almost any third-party (eg. Express, Koa, Ellipse). Anyway, you will need the matching API Core provider package.
In this demo we will use
index.ts file and add the following:
;;app.get'/hello',;app.listen3333,console.log'API Core DEMO - Listening on port 3333...';
package.json. Add the following to specify the correct entry
point and start script:
It's time to test your server:
$ yarn start
Now you can navigate to
http://localhost:3333/hello in a browser.
Creating the API and providing it via the server
First add our API definition after the hello edge, but before the call to listen:
;;;;api.version'2.0'.edgeDirpath.join__dirname, 'src/edges'// .relationDir(path.join(__dirname, 'src/relations'))// .actionDir(path.join(__dirname, 'src/actions'));
Now we have an API, continue with the configuration of Express. We need to setup body parsing, handle Chrome's requests for favicon and allow access from client scripts and disable caching with the appropriate headers:
Now we are ready to provide our API via Express:
Before you could start the API, ypu have to create an edge first.