Koa: How to create https server and support wss use koa2? I need it very much.

Created on 17 Apr 2017  Â·  5Comments  Â·  Source: koajs/koa

http + https =

const Koa = require('koa');
const app = new Koa();
http.createServer(app.callback()).listen(80);
const options = {
    key: fs.readFileSync('./ssl/private.pem', 'utf8'),
    cert: fs.readFileSync('./ssl/20180303.crt', 'utf8')
};
https.createServer(options, app.callback()).listen(443);

http + ws =

const Koa = require('koa');
const websockify = require('koa-websocket');
const app = websockify(new Koa());
app.listen(80);

https + wss = ?

question

Most helpful comment

In anyway, you can perfectly stick with HTTP on the Koa side and use a reverse-proxy (nginx, httpd) to enable HTTPS — I think most people use Koa this way (at least I use Koa this way).

All 5 comments

Any solution you might found?

@avihaymenahem No solution now, need somebody to develop a package to support it (https + wss).

In anyway, you can perfectly stick with HTTP on the Koa side and use a reverse-proxy (nginx, httpd) to enable HTTPS — I think most people use Koa this way (at least I use Koa this way).

@motet-a, Not use nginx, only a single ECS.

you can do it this way:

const server = new Koa();

// ... middlewares and routes

const options = {
   // Local certificates, if you don;t have them generate from mkcert or letsEncrypt
    key: fs.readFileSync("localhost-key.pem"),
    cert: fs.readFileSync("localhost.pem")
  };

  https.createServer(options, server.callback()).listen(port);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

rowild picture rowild  Â·  4Comments

koalex picture koalex  Â·  3Comments

tvq picture tvq  Â·  4Comments

dounine picture dounine  Â·  4Comments

xinshouke picture xinshouke  Â·  4Comments