UNPKG

@vtex/fsp-local

Version:

53 lines 1.72 kB
// server.ts import { loadConfig } from "@vtex/fsp-config"; import express from "express"; import { createProxyMiddleware } from "http-proxy-middleware"; var ROOT_PORT = 3e3; var app = express(); async function start(storeCandidate) { var _a; const { stores } = await loadConfig(); const store = storeCandidate || ((_a = Object.keys(stores)) == null ? void 0 : _a[0]); const foundStore = stores[store]; if (!foundStore) { throw new Error( `Could not find store "${store}". Make sure the store is configured on faststore.json` ); } if (!Object.keys(foundStore).length) { throw new Error( `No available modules for "${store}". Make sure the modules are properly configured on faststore.json` ); } const { discovery, checkout } = foundStore; const salesApp = foundStore["sales-app"]; if (discovery) { const discoveryProxy = createProxyMiddleware({ target: `http://localhost:${discovery.port}`, pathFilter: (path) => !path.startsWith("/checkout"), logger: process.env.FS_DEBUG ? console : void 0 }); app.use("/", discoveryProxy); app.use("/discovery", discoveryProxy); } if (checkout) { const checkoutProxy = createProxyMiddleware({ target: `http://localhost:${checkout.port}/checkout`, changeOrigin: true, logger: process.env.FS_DEBUG ? console : void 0 }); app.use("/checkout", checkoutProxy); } if (salesApp) { app.use("/sales-app", (_, res) => { return res.redirect(`http://localhost:${salesApp.port}`); }); } app.listen(ROOT_PORT, () => { console.log(`Running "${store}" and listening on port ${ROOT_PORT}`); }); } export { start }; //# sourceMappingURL=index.mjs.map