Static site hosting via tangled
1import { PagesService } from "./pages-service.js"; 2import express from "express"; 3import yargs from "yargs"; 4import { Handler } from "./handler.js"; 5import { Config } from "./config.js"; 6 7class Server { 8 constructor({ handler }) { 9 this.handler = handler; 10 11 this.app = express(); 12 13 this.app.get("/{*any}", async (req, res) => { 14 const host = req.hostname; 15 const path = req.path; 16 const { status, content, contentType } = await this.handler.handleRequest( 17 { 18 host, 19 path, 20 } 21 ); 22 res.status(status).set("Content-Type", contentType).send(content); 23 }); 24 } 25 26 async start() { 27 this.app.listen(3000, () => { 28 console.log("Server is running on port 3000"); 29 }); 30 this.app.on("error", (error) => { 31 console.error("Server error:", error); 32 }); 33 } 34} 35 36async function main() { 37 const args = yargs(process.argv.slice(2)).parse(); 38 const configFilepath = args.config || "config.json"; 39 const config = await Config.fromFile(configFilepath); 40 const handler = await Handler.fromConfig(config); 41 const server = new Server({ 42 handler, 43 }); 44 await server.start(); 45} 46 47main();