18 Nov 2022 · 5 min read

Kecil, part 5 - the API and the redirect

Endpoints live in router/api.js, which opens its own database handle from the .env config and exports an Express router.

The Kecil project folder structure in VS Code
Where everything sits by this point: packages/db for queries, router/api.js for endpoints, template for the EJS views, app.js holding the redirect.

List a user's links

router.get("/user/links", (req, res) => {
  db.getXbyY("link", "owner_id", req.user.id, (err, result) => {
    res.json({ error: err, result })
  })
})

The rest

  • POST /link/add - create a short link for the logged-in user
  • POST /link/delete - delete one, scoped to the owner
  • GET /link/random - suggest an unused slug for the form

The redirect

The catch-all in app.js is the product. Look up source_id, and either 302 to target_url or render the error page. Everything else in this series is scaffolding around that one lookup.

Share this

← All writing