UNPKG

vjsrouter

Version:

A modern, file-system based router for vanilla JavaScript with SSR support.

1 lines 4.83 kB
{"version":3,"file":"server.cjs","sources":["../src/server/index.js"],"sourcesContent":["// File: src/server/index.js\n\nconst path = require('path');\nconst fs = require('fs');\nconst express = require('express'); // The web server framework.\nconst { VJSServerRouter } = require('./VJSServerRouter.js');\nconst { renderComponentTree, renderFullPage } = require('./renderer.js');\n\n/**\n * Creates and configures an Express.js application for Server-Side Rendering with vjsrouter.\n * @param {Object} options - Configuration options.\n * @param {string} options.projectRoot - The absolute path to the root of the user's project.\n * @returns {express.Application} A fully configured Express app instance.\n */\nfunction createVjsSsrServer(options) {\n const app = express();\n\n // --- Robust Path Configuration ---\n // Use the provided projectRoot or default to the current working directory.\n const projectRoot = options.projectRoot || process.cwd();\n const manifestPath = path.join(projectRoot, 'routes.json');\n const staticAssetsPath = path.join(projectRoot); // Serve static files from the root\n const templatePath = path.join(projectRoot, 'index.html');\n\n // --- 1. Initialize the Server-Side Router ---\n const serverRouter = new VJSServerRouter({ manifestPath });\n\n // --- 2. Serve Static Assets ---\n // This middleware serves files like CSS, client-side JS, images, etc.\n // It's crucial that this comes *before* the SSR middleware.\n app.use(express.static(staticAssetsPath));\n\n // --- 3. The Core SSR Middleware ---\n // This handles all other GET requests, assuming they are for pages.\n app.get('*', async (req, res) => {\n try {\n // --- A. Resolve the Route and Fetch Data ---\n const resolution = await serverRouter.resolve(req.path);\n\n // --- B. Render the Component Tree to an HTML string ---\n const { appHtml, initialData } = await renderComponentTree(projectRoot, resolution);\n\n // --- C. Read the HTML template ---\n const template = fs.readFileSync(templatePath, 'utf-8');\n\n // --- D. Inject rendered HTML and data into the template ---\n const finalHtml = renderFullPage(template, appHtml, initialData);\n\n // --- E. Send the final HTML to the browser ---\n // We check if the route was a 404 to send the correct HTTP status code.\n if (resolution.route.path === '/404') {\n res.status(404).send(finalHtml);\n } else {\n res.status(200).send(finalHtml);\n }\n\n } catch (error) {\n // --- F. Robust Error Handling ---\n // If anything fails during the SSR process, log it and send a 500 error.\n console.error(`[SSR Middleware] Failed to render path ${req.path}:`, error);\n res.status(500).send('<h1>500 - Internal Server Error</h1><p>Something went wrong on our end. Please try again later.</p>');\n }\n });\n\n return app;\n}\n\nmodule.exports = { createVjsSsrServer };\n"],"names":[],"mappings":";;AAAA;AACA;AACA,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAC7B,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AACzB,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;AACnC,MAAM,EAAE,eAAe,EAAE,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAAC;AAC5D,MAAM,EAAE,mBAAmB,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;AACzE;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS,kBAAkB,CAAC,OAAO,EAAE;AACrC,EAAE,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;AACxB;AACA;AACA;AACA,EAAE,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;AAC3D,EAAE,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,aAAa,CAAC,CAAC;AAC7D,EAAE,MAAM,gBAAgB,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;AAClD,EAAE,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;AAC5D;AACA;AACA,EAAE,MAAM,YAAY,GAAG,IAAI,eAAe,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC;AAC7D;AACA;AACA;AACA;AACA,EAAE,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC,CAAC;AAC5C;AACA;AACA;AACA,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,E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