Deno 1.1.1 静态 HTTP 服务器
Deno 1.1.1 Static HTTP Server
我已经阅读了几个小时有关 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
阅读 serveTLS and listenAndServeTLS 上的文档。
如果您有有效的证书,应该不会有任何问题。但是,我在尝试以本地用户身份 运行 HTTPS 服务器时遇到了一些困难 overriding rejections of self-signed SSL certificates. Also, I ended up serving ssl on a port other than 443, due to permission errors。
这些是我 运行 遇到的问题以及我是如何解决这些问题的。希望对你有帮助。
我已经阅读了几个小时有关 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
阅读 serveTLS and listenAndServeTLS 上的文档。
如果您有有效的证书,应该不会有任何问题。但是,我在尝试以本地用户身份 运行 HTTPS 服务器时遇到了一些困难 overriding rejections of self-signed SSL certificates. Also, I ended up serving ssl on a port other than 443, due to permission errors。
这些是我 运行 遇到的问题以及我是如何解决这些问题的。希望对你有帮助。