let axios = require('axios'); let express = require('express'); let builderApiKey = 'bb209db71e62412dbe0114bdae18fd15'; let app = express(); app.get('/', (req, res) => { res.send( template({ body: `
This page comes from our code.
` }) ); }); app.get('/about', (req, res) => { res.send( template({ body: `This page comes from our code too.
`, }) ); }); // Put this route last, so you will catch anything not matched by your code app.get('*', async (req, res) => { let page = await axios .get( `https://cdn.builder.io/api/v1/html/page?url=${encodeURI(req.url)}&apiKey=${builderApiKey}` ) .catch(handleError); if (page && page.data) { res.send(template({ body: 'Have you published a Builder page for this URL?
`, }) ); } }); app.listen(3000, () => { console.log('Listening on port 3000...'); }); let handleError = err => { if (err.response.status === 404) { // Catch 404s - no page was found for this URL, that's fine } else { console.warn(err); } return null; }; // Basic function to render content within a standard header and footer // You can use any templating system you choose let template = ({ body, title }) => `