SPB Git

Repository Public

server.mjs 4.2 KB 103 lines javascript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : QYVORA8 * File             : site-server/server.mjs9 * Module           : Site Server10 * Repository       : https://git.spboucher.ai/qyvora11 * Project Website  : Not specified12 * Created          : 2026-12-0413 * Last Modified    : 2026-12-0414 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243// Static file server for qyvora.spboucher.ai. No dependency, no cookie, no log of visitor addresses.44import { createServer } from 'node:http';45import { readFile, stat } from 'node:fs/promises';46import { extname, join, normalize, resolve } from 'node:path';47import { fileURLToPath } from 'node:url';4849const root = resolve(fileURLToPath(new URL('./public', import.meta.url)));50const port = Number(process.argv[2] || process.env.PORT || 8790);51const types = {52  '.html': 'text/html; charset=utf-8', '.css': 'text/css; charset=utf-8', '.svg': 'image/svg+xml',53  '.png': 'image/png', '.ico': 'image/x-icon', '.txt': 'text/plain; charset=utf-8', '.json': 'application/json',54};55const security = {56  'Content-Security-Policy': "default-src 'self'; script-src 'none'; object-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'",57  'X-Content-Type-Options': 'nosniff',58  'Referrer-Policy': 'no-referrer',59  'Strict-Transport-Security': 'max-age=31536000; includeSubDomains',60  'Permissions-Policy': 'camera=(), microphone=(), geolocation=(), interest-cohort=()',61  'Cross-Origin-Opener-Policy': 'same-origin',62};6364async function send(res, status, file, head) {65  const body = await readFile(file);66  const html = extname(file) === '.html';67  res.writeHead(status, {68    ...security,69    'Content-Type': types[extname(file)] || 'application/octet-stream',70    'Content-Length': body.length,71    'Cache-Control': html ? 'no-cache' : 'public, max-age=86400',72  });73  res.end(head ? undefined : body);74}7576createServer(async (req, res) => {77  try {78    if (req.method !== 'GET' && req.method !== 'HEAD') {79      res.writeHead(405, { ...security, Allow: 'GET, HEAD' });80      return res.end();81    }82    const head = req.method === 'HEAD';83    let path = decodeURIComponent(new URL(req.url, 'http://x').pathname);84    if (path === '/healthz') {85      res.writeHead(200, { ...security, 'Content-Type': 'text/plain; charset=utf-8', 'Cache-Control': 'no-store' });86      return res.end('ok\n');87    }88    if (path.endsWith('/')) path += 'index.html';89    let file = normalize(join(root, path));90    if (file !== root && !file.startsWith(root + '/')) file = join(root, '404.html');91    let info = await stat(file).catch(() => null);92    if (!info && !extname(file)) {93      file += '.html';94      info = await stat(file).catch(() => null);95    }96    if (!info || !info.isFile()) return await send(res, 404, join(root, '404.html'), head);97    await send(res, 200, file, head);98  } catch {99    res.writeHead(400, security);100    res.end();101  }102}).listen(port, '0.0.0.0', () => console.log(`qyvora site on :${port}`));103
Files main
Menu

Theme