npm install
$ sequelize db:migrate
- setup DB structure$ sequelize db:seed:all
- to load csv datanpm run dev
- start service in dev mode with nodemonnpm test
- run tests
This REST API includes the following:
- Fetch a recipe by id
GET /recipes/:recipe_id
GET /recipes/:recipe_id?embed=ratings
- parameters:
- recipe_id
- embed=ratings - display embeded (includes) list ratings object
- Fetch all recipes for a specific cuisine with pagination
GET /recipes
- defaults query params to?page=0&per_page=10
GET /recipes?cuisine=&page=0&per_page=2
- parameters:
page
- current page, 0 based index, defaults to 0per_page
- number of results per page, defaults to 10cuisine
- filter based onrecipe_cuisine
field
- pagination:
- Link headers - this allows metadata to be given for pagination without affecting the API request. Relation Type in the
rel
Link header is used to representfirst
,next
,prev
, orlast
Url Links.
- Link headers - this allows metadata to be given for pagination without affecting the API request. Relation Type in the
- Rate an existing recipe between 1 and 5
PUT /recipes/rate/:recipe_id
- parameters:
- recipe_id
- Update an existing recipe
PUT /recipes/:recipe_id
- parameters:
- recipe_id
- Store a new recipe
POST /recipes
- mandatory fields:
box_type
title
recipe_cuisine
Nodejs REST API
- restify - Node.js web service framework to build RESTful API
- sqlite - to temporarily store recipes and ratings in lightweight database file with option to store i-memory
- sequelize - ORM for Node.js to support SQLite queries via object models
- ava - test runner for unit test and integration tests
- supertest - integration tests agent for Node.js to test single API endpoint
As a RESTful API, clients/consumers will be able to access the above endpoints. Each API consumers should register for an API app_key
in order for this service to track and log incoming API requests. Providing an app_key
in the header of each API requests is common practice.
Example of API consumer can be a mobile app, web app, or another web service or a GraphQL server.
The Pagination feature in the GET /recipes
allows mobile apps to provide a scroll feature to dynamically load the next set of recipes list.
Providing the auth_token
in the Authorization header allows this service validate the credentials of the end-user request. In this example, we could identify which users have added a rating to a specific recipe and prevent a single user to submit multiple recipe ratings on a single recipe.
In this example, we use mysql as a database instance for production environment. Before starting the service for the first time, please ensure to load the migration files and optionally seeds
$ sequelize db:create --env production
- create database specified by the configuration under db/config.js$ sequelize db:migrate --env production
- run pending migrations, setup DB structure$ sequelize db:seed:all --env production
- run seeder file to load csv datanpm start
- start service
As a Nodejs application, it is relatively straight-forward to deploy onto Google Cloud Platform AppEngine using Travis-CI.