#!/usr/bin/env node const SpaccDotWebServer = require('../SpaccDotWeb.Server.js'); const server = SpaccDotWebServer.setup({ appName: 'Example', // staticPrefix: '/static/', // staticRoot: '', // not (yet) implemented // staticFiles: [], linkStyles: [ 'index.css' ], // linkRuntimeScripts: [], // not (yet) implemented linkClientScripts: [ 'particles.js' ], // pageTitler: (title, opts={}, context) => `...`, // appPager: (content, title, opts={}, context) => `...`, // htmlPager: (content, title, opts={}, context) => `...`, }); if (SpaccDotWebServer.envIsNode && ['dump', 'html', 'writeStaticHtml'].includes(process.argv[2])) { const fileName = server.writeStaticHtml(Number(process.argv[3] || 0)); console.log(`Dumped Static HTML to '${fileName}'!`); } else { const serverData = server.initServer({ // defaultResponse: { code: 500, headers: {} }, // endpointsFalltrough: false, // port: 3000, address: '0.0.0.0', // maxBodyUploadSize: null, // handleHttpHead: true, // appElement: 'div#app', // transitionElement: 'div#transition', // metaCookie: 'spaccdotweb-meta', // cookieInUrl: 'spaccdotweb-cookie', // not (yet) implemented // endpoints are defined by a discriminator and an action endpoints: [ // a discriminator can be a simple boolean function [ (ctx) => { const now = (new Date); return (['GET', 'POST'].includes(ctx.request.method) && now.getHours() === 0 && now.getMinutes() === 0); }, (ctx) => ctx.renderPage(`
We're sorry but, to avoid disturbing the spirits, Testing is not available at 00:00. Please retry in just a minute.
`, 'Error') ], // or, a discriminator can be a specially-constructed filter string [ 'GET|POST /main/', async (ctx) => { //[ (ctx) => (['GET', 'POST'].includes(ctx.request.method) && ctx.urlSections[0] === 'main'), (ctx) => { if (ctx.request.method === 'POST') { if (ctx.bodyParameters?.add) { ctx.setCookie(`count=${parseInt(ctx.getCookie('count') || 0) + 1}`); } else if (ctx.bodyParameters?.reset) { ctx.setCookie(`count=`); } // a short sleep so that we can test client transitions await (new Promise(resolve => setTimeout(resolve, 1500))); } const content = `This page was rendered at ${Date()}.
These were your cookies at time of request:
${ctx.getCookie() || '[None]'}
Context data for this request:
${JSON.stringify({ request: { method: ctx.request.method, }, urlSections: ctx.urlSections, urlParameters: ctx.urlParameters, bodyParameters: ctx.bodyParameters, }, null, 2)}`; // the main content of a page can be rendered with the main template using: ctx.renderPage(content, 'Test'); } ], // redirects are easy [ 'GET', (ctx) => { ctx.redirectTo('/main/'); // alternatively: return { code: 302, headers: { location: '/main/' } }; } ], ], }); if (SpaccDotWebServer.envIsNode) { console.log(`Running Server on <${serverData.address}:${serverData.port}>...`); } };