Node.js: отправка HTML-страницы в браузер

Чтобы отправить HTML-страницу в браузер с помощью Node.js, вы можете использовать несколько методов. Вот несколько примеров:

  1. Использование модуля HTTP:

    const http = require('http');
    const fs = require('fs');
    http.createServer((req, res) => {
    fs.readFile('path/to/your/html/file.html', (err, data) => {
    if (err) {
      res.writeHead(404, { 'Content-Type': 'text/html' });
      return res.end('404 Not Found');
    }
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.write(data);
    return res.end();
    });
    }).listen(3000);
  2. Использование платформы Express:

    const express = require('express');
    const app = express();
    app.get('/', (req, res) => {
    res.sendFile('path/to/your/html/file.html');
    });
    app.listen(3000, () => {
    console.log('Server is running on port 3000');
    });
  3. Использование платформы Koa:

    const Koa = require('koa');
    const app = new Koa();
    const fs = require('fs');
    app.use(async (ctx) => {
    const html = await fs.promises.readFile('path/to/your/html/file.html', 'utf-8');
    ctx.body = html;
    });
    app.listen(3000, () => {
    console.log('Server is running on port 3000');
    });

Это всего лишь несколько примеров того, как можно отправить HTML-страницу в браузер с помощью Node.js. Доступны и другие платформы и библиотеки, такие как Hapi и Fastify, которые предоставляют аналогичную функциональность.