【问题标题】:Deno 1.1.1 Static HTTP ServerDeno 1.1.1 静态 HTTP 服务器
【发布时间】:2020-09-21 00:08:49
【问题描述】:

我已经阅读了几个小时关于 Deno 的文章,终于运行了一个 http 静态服务器。

我想知道添加 https 还需要什么。

我了解从叶到根的证书安排,但在 Deno 中不了解。

工作代码:

import {
  gray,
  green,
  cyan,
  bold,
  yellow,
  red,
} from 'https://deno.land/std@0.58.0/fmt/colors.ts';

import { Application, HttpError, send, Status } from 'https://deno.land/x/oak/mod.ts';

const app = new Application();

// Middleware 1 - Error handler
app.use(async (context, next) => {
  try {
    await next();

  } catch (e) {

    if (e instanceof HttpError) {
      context.response.status = e.status as any;
      // expose
      // Determines if details about the error should be automatically exposed in a response.
      // This is automatically set to true for 4XX errors, as they represent errors in the request...
      if (e.expose) {
        context.response.body = `
        <!DOCTYPE html>
        <html>
          <body>
            <h1>${e.status} - ${Status[e.status]}</h1>
            <!-- <h1>${e.status} - ${e.message}</h1> -->
          </body>
        </html>`;
      } else {
        context.response.body = `
        <!DOCTYPE html>
        <html>
          <body>
            <h1>${e.status} - ${Status[e.status]}</h1>
            <!-- <h1>${e.status} - ${e.message}</h1> -->
          </body>
        </html>`;
      }

    } else if (e instanceof Error) {
      context.response.status = 500;
      // ...while 5XX errors are set to false as they are internal server errors and 
      // exposing details could leak important server security information.
      context.response.body = `
      <!DOCTYPE html>
      <html>
        <body>
          <h1>500 - Internal Server Error</h1>
        </body>
      </html>`;
      console.log('Unhandled Error:', red(bold(e.message)));
      console.log(e.stack);
    }
  }
});

// Middleware 2 - Logger
app.use(async (context, next) => {
  await next();
  const rt = context.response.headers.get('X-Response-Time');
  console.log(`${green(context.request.method)} ${cyan(context.request.url.pathname)} - ${bold(String(rt),)}`, );
});

// Middleware 3 - Response Time
app.use(async (context, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  context.response.headers.set('X-Response-Time', `${ms}ms`);
});

// End point - Send static content
app.use(async (context) => {
  await context.send({
    root: `${Deno.cwd()}/var/www/example1.com/public`,
    index: 'index.html',
  });
});

// Welcome message
app.addEventListener('listen', ({ hostname, port }) => {

  console.clear();

  console.log(' ');
  console.log(bold('Deno 1.1.1 - Static HTTP Server'));
  console.log(gray('  @host: ') + yellow(`${hostname}`));
  console.log(gray('  @port: ') + yellow(`${port}`));
  console.log(gray(' Status: ') + green('online'));
  console.log(gray('  @root: ') + cyan('/var/www/example1.com/public'));
  console.log(gray(' @index: ') + cyan('index.html'));
  console.log(' ');

});

await app.listen({ hostname: '127.0.0.1', port: 80 });

// run the server
// deno run --allow-net --allow-read httpServer.ts

【问题讨论】:

    标签: node.js ssl https deno


    【解决方案1】:

    阅读serveTLSlistenAndServeTLS 上的文档。

    如果您拥有有效的证书,则应该没有任何问题。但是,我遇到了一些困难overriding rejections of self-signed SSL certificates。另外,由于我在尝试以本地用户身份运行 HTTPS 服务器时收到了permission errors,因此我最终在 a port other than 443 上提供了 ssl。

    这些是我遇到的问题以及我是如何解决这些问题的。希望对你有帮助。

    【讨论】:

    • 现在在 ubuntu 19.04 中测试 http 服务器并获得很多权限被拒绝。它适用于 Windows。
    • 我刚刚以 root 身份尝试过,它可以工作。我无法让它以 sudo 用户身份运行。
    • 而且...我终于明白为什么你仍然想使用 NGINX 作为代理服务器。如果你来自 NGINX + PHP-FPM,Deno 将取代 PHP-FPM。从 1.1.1 开始,Deno 在 https 方面根本无法与 NGINX 竞争。在我对其进行 ssl 测试后,deno https 服务器崩溃了;只是为 ssl 测试它就崩溃了。 NGINX 是一头野兽;一台高大、精瘦、简陋的机器,坚固耐用,经过时间考验,C 善良。他们可以更好地协同工作,作为另一个代理。
    • @suchislife:我同意。 Deno SSL 在版本 1 中的黄金时间还没有准备好。对我来说,deno 只是一个玩具,直到它允许开发人员决定是什么让证书“坏”(就像 node.js 允许的那样)。此外,Nginx 在这一点上绝对提供了卓越的性能(当涉及到 HTTPS 时)。
    猜你喜欢
    • 2014-06-29
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    相关资源
    最近更新 更多