// A starter Garbo mini app's server, in Node.js 18 or newer (no packages): it serves // index.html and checks the launch data your page sends (POST /api/me). // // GARBO_APP_SECRET= node server.mjs // // Then open http://localhost:8080 in a browser (it says "Not opened in Garbo"), or register // your app as a draft with your computer's address (or a tunnel's) and open it in Garbo. import { createServer } from 'node:http'; import { readFileSync } from 'node:fs'; import { checkHash } from '../check/node.mjs'; const PORT = Number(process.env.PORT ?? 8080); const APP_SECRET = process.env.GARBO_APP_SECRET ?? ''; const page = readFileSync(new URL('./index.html', import.meta.url)); createServer(async (req, res) => { if (req.method === 'POST' && req.url === '/api/me') { let body = ''; for await (const chunk of req) body += chunk; res.setHeader('Content-Type', 'application/json'); try { // The hash proves Garbo made this launch data for your app, for this person, just now. const data = checkHash(body, APP_SECRET); // data.user.id is the person's id in your app: keep their cart, orders and so on under it. return res.end(JSON.stringify({ user: data.user, startParam: data.start_param ?? null })); } catch (error) { res.statusCode = 401; return res.end(JSON.stringify({ error: error.message })); } } res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.end(page); }).listen(PORT, () => console.log(`Your mini app: http://localhost:${PORT}`));