Documentation
HTTP & Caching Layer
HTTP & Cache
Communicate with external REST services via the high-speed compiled HTTP driver, and save latency with declarative route caching.
01
HTTP Client
Fetch remote resources using the native http module. Parse response payloads directly into JSON models.
fetch.js
import { http } from 'kitwork';
const fetch = http.get("https://api.external.com/data");
if (fetch.status == 200) {
const body = fetch.json();
log.Print("Parsed API response: " + body.message);
}
const fetch = http.get("https://api.external.com/data");
if (fetch.status == 200) {
const body = fetch.json();
log.Print("Parsed API response: " + body.message);
}
02
Declarative Caching
Speed up heavy operations or external database reads by attaching a cache directive directly to your route declarations.
cache.js
// Cache responses for 1 hour
router.get("/heavy-data").cache("1h").handle((res) => {
const data = db.table("analytics").list();
return res.json(data);
});
router.get("/heavy-data").cache("1h").handle((res) => {
const data = db.table("analytics").list();
return res.json(data);
});
03
Route Flow Hooks
Define success/failure hooks using .then() and .catch() methods to implement clean logging, error metrics, and response monitors.
hooks.js
router.get("/external-sync")
.catch(() => log.Print("External sync failed"))
.then(() => log.Print("External sync succeeded"))
.handle((res) => {
const fetch = http.get("https://api.external.com/sync");
return res.json(fetch.json());
});
.catch(() => log.Print("External sync failed"))
.then(() => log.Print("External sync succeeded"))
.handle((res) => {
const fetch = http.get("https://api.external.com/sync");
return res.json(fetch.json());
});