Node.js Deno 1.1.1静态HTTP服务器

Node.js Deno 1.1.1静态HTTP服务器,node.js,ssl,https,deno,Node.js,Ssl,Https,Deno,我读了几个小时关于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 'ht

我读了几个小时关于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
导入{
灰色
绿色
青色,
大胆的
黄色的,
红色
}从'https://deno.land/std@0.58.0/fmt/colors.ts';
从导入{Application,HttpError,send,Status}https://deno.land/x/oak/mod.ts';
const app=新应用程序();
//中间件1-错误处理程序
应用程序使用(异步(上下文,下一步)=>{
试一试{
等待下一个();
}捕获(e){
if(HttpError的实例){
context.response.status=e.status,如有;
//暴露
//确定是否应在响应中自动公开有关错误的详细信息。
//对于4XX错误,这会自动设置为true,因为它们表示请求中的错误。。。
如果(例如暴露){
context.response.body=`
${e.status}-${status[e.status]}
`;
}否则{
context.response.body=`
${e.status}-${status[e.status]}
`;
}
}else if(e instanceof Error){
context.response.status=500;
//…而5XX错误设置为false,因为它们是内部服务器错误和
//公开详细信息可能会泄露重要的服务器安全信息。
context.response.body=`
500-内部服务器错误
`;
log('未处理的错误:',红色(粗体(e.message));
console.log(e.stack);
}
}
});
//中间件2-记录器
应用程序使用(异步(上下文,下一步)=>{
等待下一个();
const rt=context.response.headers.get('X-response-Time');
log(`${green(context.request.method)}${cyan(context.request.url.pathname)}-${bold(String(rt),)}`,);
});
//中间件3-响应时间
应用程序使用(异步(上下文,下一步)=>{
const start=Date.now();
等待下一个();
const ms=Date.now()-开始;
set('X-response-Time',`${ms}ms`);
});
//端点-发送静态内容
应用程序使用(异步(上下文)=>{
等待上下文。发送({
根:`${Deno.cwd()}/var/www/example1.com/public`,
索引:“index.html”,
});
});
//欢迎辞
app.addEventListener('listen',({hostname,port})=>{
console.clear();
控制台日志(“”);
log(粗体('Deno 1.1.1-静态HTTP服务器');
log(灰色('@host:')+黄色('${hostname}');
log(灰色('@port:')+黄色('${port}');
log(灰色('Status:')+绿色('online');
log(灰色('@root:')+青色('/var/www/example1.com/public');
log(灰色('@index:')+青色('index.html');
控制台日志(“”);
});
等待app.listen({hostname:'127.0.0.1',端口:80});
//运行服务器
//deno run--allow net--allow read httpServer.ts
阅读和上的文档

如果你有一个有效的证书,你不应该有任何问题。然而,我遇到了一些困难。此外,由于我在尝试以本地用户身份运行HTTPS服务器时收到了ssl,因此我最终在上提供ssl服务


这些就是我遇到的问题,以及我是如何解决这些问题的。我希望这会有所帮助。

现在就在ubuntu 19.04中测试http服务器,但很多权限被拒绝。它可以在windows下工作。我刚刚试过它作为根目录,它可以工作。我无法让它以sudo用户的身份运行。而且。。。我终于明白了为什么您仍然想使用NGINX作为代理服务器。如果您来自
NGINX+PHP-FPM
Deno将取代
PHP-FPM
。从1.1.1开始,在
https
方面,Deno根本无法与NGINX竞争。在我对deno https服务器运行ssl测试后,它崩溃了;只是测试它的ssl崩溃了它。NGINX是一只野兽;一台高大、瘦削、粗犷、久经考验、善良的机器。他们可以更好地合作,一个代替另一个。@suchislife:我同意。Deno SSL在版本1中还没有准备好进入黄金时间。对我来说,deno只是一个玩具,直到它允许开发人员决定证书“坏”的原因(如node.js允许)。另外,Nginx在这一点上提供了卓越的性能(当涉及到HTTPS时)。