import { join } from “path”;
import { readFileSync } from “fs”;
import express from “express”;
import serveStatic from “serve-static”
import shopify from “./shopify.js”;
import GDPRWebhookHandlers from “./gdpr.js”;
const PORT = parseInt(process.env.BACKEND_PORT || process.env.PORT, 10);
const STATIC_PATH =
process.env.NODE_ENV === “production”
? ${process.cwd()}/frontend/dist
: ${process.cwd()}/frontend/;
const app = express();
app.get(shopify.config.auth.path, shopify.auth.begin());
app.get(
shopify.config.auth.callbackPath,
shopify.auth.callback(),
shopify.redirectToShopifyOrAppRoot()
);
app.post(
shopify.config.webhooks.path,
shopify.processWebhooks({ webhookHandlers: GDPRWebhookHandlers })
);
app.use(“/api/*”, shopify.validateAuthenticatedSession());
app.use(express.json());
app.get(“/api/products/count”, async (req, res) => {
console.log(“inside /api/products/count”); // 1. trying to reach here to fetch data from graphql and pass it to UI.
// const countData = await shopify.api.rest.Product.count({
// session: res.locals.shopify.session, // 2. also not sure what this is
// });
res.status(200).send(10);
});
app.use(shopify.cspHeaders());
app.use(serveStatic(STATIC_PATH, { index: false }));
app.use(“/*”, shopify.ensureInstalledOnShop(), async (_req, res, _next) => {
return res
.status(200)
.set(“Content-Type”, “text/html”)
.send(readFileSync(join(STATIC_PATH, “index.html”)));
});
console.log("App Running on ", PORT);
app.listen(PORT);
I have created above code using shopify cli.
I have setup redis storage, I can see key’s inside redis so access token is getting stored.
Also from UI side wherever I call /api/products/count API, it does have valid JWT token in Authorization key of header.