Strumenti Utente

Strumenti Sito


programmazione:bun:http_server_intro

Differenze

Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.

Link a questa pagina di confronto

Entrambe le parti precedenti la revisioneRevisione precedente
Prossima revisione
Revisione precedente
programmazione:bun:http_server_intro [04/07/2026 15:59] – Fabio Di Matteoprogrammazione:bun:http_server_intro [13/07/2026 11:23] (versione attuale) – Fabio Di Matteo
Linea 10: Linea 10:
 import {lista} from "./lista.js"; import {lista} from "./lista.js";
  
-function echo (s){+function echo (s,status=200){
     let myheader={"Content-Type": "text/html; charset=utf-8",}       let myheader={"Content-Type": "text/html; charset=utf-8",}  
-    return new Response(s,{headers: myheader});+    return new Response(s,{headers: myheader, status: status});
 } }
 +
 +
  
  
Linea 59: Linea 61:
  
  
 +
 +</code>
 +
 +===== Uso di template ejs =====
 +
 +<code javascript>
 +#!/usr/bin/env bun
 +
 +
 +import ejs from './ejs/ejs.js';
 +
 +function echo (s,status=200){
 +    let myheader={"Content-Type": "text/html; charset=utf-8",}  
 +    return new Response(s,{headers: myheader, status: status});
 +}
 +
 +
 +
 +
 +
 +async function home(){
 +      const template = await Bun.file("./views/home.html").text();
 +
 +      const html = ejs.render(template, {
 +        user: "Fabio Di Matteo",
 +      });
 +    return echo(html);
 +}
 +
 +
 +const server = Bun.serve({
 +  routes: {
 +    "/": home,
 +    
 +  }
 +});      
 +console.log(`Server running at ${server.url}`);
 +
 +</code>
 +
 +**views/home.html**
 +<code html>
 +
 +<!DOCTYPE html>
 +<html>
 +<head>
 +<meta name="viewport" content="width=device-width, initial-scale=1.0" />
 +<title>Hello</title>
 +</head>
 +<body>
 + <h1><%= user  %> </h1>
 +</body>
 +</html> 
  
 </code> </code>
programmazione/bun/http_server_intro.1783173571.txt.gz · Ultima modifica: 04/07/2026 15:59 da Fabio Di Matteo