Skip to content

What's the proper way to access database/api? #42

Open
@UROjQ6r80p

Description

@UROjQ6r80p

Let's say I want to fetch products from the database.

that's the old way:

import express from "express";
import markoPlugin from "@marko/express";
import template from "./template.marko";

const app = express();
app.use(markoPlugin()); // Enables `res.marko(template, input)`

app.get("/", function (req, res) {
  const products = req.db.find(...)

  res.marko(template, {
    products
  });
});

app.listen(8080);

What's the proper way to do the same in marko/run?

I tried something like this in +handler.js

export const GET: MarkoRun.Handler = ({ request, platform }, next) => {
    platform.response.locals = {
        data: Math.random()
    }

    next()
};

and then in +page.marko
<h1>${$global.platform.response.locals.data}</h1>
but i'm not sure it's the best way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions