gpt4 book ai didi

node.js - Deno 1.1.1 静态 HTTP 服务器

转载 作者:行者123 更新时间:2023-12-04 22:39:27 24 4
gpt4 key购买 nike

我已经阅读了几个小时关于 Deno 的文章,终于运行了一个 http 静态服务器。
我想知道添加 https 还需要什么。
我了解从叶到根的证书安排,但在 Deno 中不了解。
sc
工作代码:

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

最佳答案

阅读 serveTLS 上的文档和 listenAndServeTLS .
如果你有一个有效的证书,你不应该有任何问题。但是,我遇到了一些困难overriding rejections of self-signed SSL certificates .另外,我最终在 a port other than 443 上提供了 ssl , 由于 permission errors我在尝试以本地用户身份运行 HTTPS 服务器时收到。
这些是我遇到的问题以及我是如何解决这些问题的。我希望它有所帮助。

关于node.js - Deno 1.1.1 静态 HTTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62567208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com