Node and Express are the runtime and HTTP framework for your API. Topics here cover bootstrapping the app, parsing JSON bodies, and structured logging—everything that runs before a route handler executes. Master this layer first so later routers and controllers plug into a consistent foundation.

How to return Promises from services — Intermediate

Service functions return promises (or async functions that return data). Controllers await them; do not mix callback-style Knex with async/await in the same path.
Learn: Keep Promise-returning database code in services. The controller awaits one or more service calls and maps results to HTTP. If you return knex(...) directly from a service without await inside, the controller still awaits the same Promise—both styles work if errors propagate to next(err).

Supplementary examples

async service function

async function remove(pasteId) {
  const deleted = await knex("pastes").where({ paste_id: pasteId }).del();
  if (!deleted) {
    const err = new Error("Paste not found");
    err.status = 404;
    throw err;
  }
}

Course example

// service — return the Knex promise (or use async/await inside)
function listByUser(userId) {
  return knex("pastes")
    .where({ user_id: userId })
    .orderBy("created_at", "desc");
}

// controller
async function list(req, res, next) {
  try {
    const rows = await service.listByUser(req.params.userId);
    res.json({ data: rows });
  } catch (err) {
    next(err);
  }
}

Additional references & examples

← All Async and await in Express · Node / Express