1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Elysia/Bun】Elysiaサーバーをhttpsにする

Last updated at Posted at 2024-04-02

Elysiaサーバーを作っていて、証明書なしの http は問題なくNet上にいくらでもサンプルがあります。でも、https にしようとすると、BunについてのBun.serveによる tsl 設定方法は見つかるのだけれど、new Elysia()では2024年3月現在見当たらず試行錯誤して判ったので、簡単にメモしておこうと思います。

letsencryptを使った方法ですが、こんな感じです。

server.ts
import { Elysia } from 'elysia';
const PORT = 443;
const KEYS = {
    cert: Bun.file("/etc/letsencrypt/live/<ドメイン名>/cert.pem"),
    key: Bun.file("/etc/letsencrypt/live/<ドメイン名>/privkey.pem")
}

const app = new Elysia()
  .get('/', () => 'my https server is running!')

  .listen({
        port: PORT,
        tls: KEYS
    },  (token: any) => {
        if (token) {
            console.log(`Listening to port ${PORT}`);
        } else {
            console.error(`Failed to listen to port ${PORT}`);
        }
    })
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?