Files
internship_node/day6/routes/index.js
T

33 lines
1002 B
JavaScript
Raw Normal View History

2025-07-14 21:52:29 +01:00
var express = require("express");
2022-02-06 21:10:59 -05:00
var router = express.Router();
2025-07-14 21:52:29 +01:00
const { exec } = require("child_process");
2022-02-06 21:10:59 -05:00
/* GET home page. */
2025-07-14 21:52:29 +01:00
router.get("/", function (req, res, next) {
res.render("index", { title: "Express" });
});
router.get("/products", function (req, res, next) {
// Use curl to fetch products from Shopify
const curlCmd = `curl -X GET \"https://roving-house.myshopify.com/admin/api/2021-07/products.json\" -u 65c3c809c002379e3a0ea04aeaf2cf84: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`;
exec(curlCmd, (error, stdout, stderr) => {
if (error) {
return res.render("products", { products: [], error: error.message });
}
let products = [];
console.log(error, stdout);
try {
const data = JSON.parse(stdout);
products = data.products || [];
} catch (e) {
return res.render("products", {
products: [],
error: "Failed to parse products",
});
}
res.render("products", { products });
});
2022-02-06 21:10:59 -05:00
});
module.exports = router;