【问题标题】:Koa HTTPS connection with koa-sslify module Without Reverse ProxyKoa HTTPS 连接与没有反向代理的 koa-sslify 模块
【发布时间】:2018-09-05 15:47:30
【问题描述】:

我尝试使用该模块 https://www.npmjs.com/package/koa-sslify 为 Koa 服务器建立 HTTPS 连接,但出现错误“AssertionError: app.use() requires a generator function”

'use strict';
var app = require('application'),
    enforceHttps = require('koa-sslify'),
    config = require('config'),
    fs = require('fs'),
    path = require('path'),
    routes = fs.readdirSync(path.join(__dirname, '../routing')).sort();

routes.forEach(function (route) {
    require('../routing/' + route);
});

// Force HTTPS on all page
app.use(enforceHttps({
  trustProtoHeader: true
}));

app.listen(config.server.port,config.server.host);

更新:

我改用 NGINX,因为它可能会更好地工作并且使用更少的资源

【问题讨论】:

  • 也许还有其他方法可以做到这一点?

标签: ssl https koa


【解决方案1】:

尝试这种方式,但最好使用 NGINX 作为反向代理服务器

const Koa = require('koa');
const https = require('https');
const fs = require('fs');
const { default: enforceHttps } = require('koa-sslify');

const app = new Koa();

// Force HTTPS using default resolver
app.use(enforceHttps({
  port: 443
}));

// index page
app.use(ctx => {
  ctx.body = `hello world from ${ctx.request.url}`;
});

// SSL options
const options = {
  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.crt')
}

// start the server
https.createServer(options, app.callback()).listen(443);

【讨论】:

  • 我改用了 nginx,忘记了这个问题。不过还是谢谢。
  • 哦,我看到你写了关于 NGINX 的文章,太好了,我也这么认为,因为 NGINX 可能会更好地工作并且使用更少的资源。
猜你喜欢
  • 2017-05-01
  • 2019-07-08
  • 2015-10-21
  • 1970-01-01
  • 2015-10-08
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
相关资源
最近更新 更多