Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
520 changes: 379 additions & 141 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions rsbuild/express-https/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Local
.DS_Store
*.local
*.log*

# Dist
node_modules
dist/

# IDE
.vscode/*
!.vscode/extensions.json
.idea
35 changes: 35 additions & 0 deletions rsbuild/express-https/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Rsbuild + Express (HTTPS)

Custom [Express](https://expressjs.com/) **HTTPS** server integrated with Rsbuild's dev middlewares and HMR.

The plain HTTP version lives in [`../express`](../express). Setting only `server.https` in `rsbuild.config` is not enough for a custom Express server: `app.listen()` still creates an HTTP server. This example creates the TLS socket with `https.createServer` and passes the same certificate into Rsbuild so the client uses `https` / `wss`.

## Setup

Install the dependencies:

```bash
pnpm install
```

## Get Started

Start the HTTPS dev server:

```bash
pnpm dev
```

Open the printed `https://localhost:<port>/` URL. The certificate is self-signed (`selfsigned`), so the browser will show a warning — continue once for local development.

Build the app for production:

```bash
pnpm build
```

Preview the production build locally (Rsbuild preview server; use `server.https` or `@rsbuild/plugin-basic-ssl` if you need HTTPS for preview):

```bash
pnpm preview
```
19 changes: 19 additions & 0 deletions rsbuild/express-https/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@rsbuild-example/express-https",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
"build": "rsbuild build",
"dev": "node ./server.js",
"preview": "rsbuild preview"
},
"devDependencies": {
"@rsbuild/core": "2.2.0",
"@types/express": "^5.0.6",
"@types/node": "^24.12.4",
"express": "^5.2.1",
"selfsigned": "^5.5.0",
"typescript": "^5.9.3"
}
}
8 changes: 8 additions & 0 deletions rsbuild/express-https/rsbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
// Custom Express owns the HTTPS listen socket; keep Rsbuild in middleware mode.
server: {
middlewareMode: true,
},
});
55 changes: 55 additions & 0 deletions rsbuild/express-https/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import https from 'node:https';
import express from 'express';
import { createRsbuild, loadConfig } from '@rsbuild/core';
import selfsigned from 'selfsigned';

export async function startDevServer() {
const { content } = await loadConfig({});

// Generate an untrusted self-signed certificate for local HTTPS.
// Your browser will warn once; confirm to continue (same as plugin-basic-ssl).
const pems = await selfsigned.generate([{ name: 'commonName', value: 'localhost' }]);
const httpsOptions = {
key: pems.private,
cert: pems.cert,
};

// Pass the same HTTPS options into Rsbuild so asset/HMR URLs use https/wss.
// Do not rely on `server.https` alone with `app.listen()` — that stays HTTP.
const rsbuild = await createRsbuild({
rsbuildConfig: {
...content,
server: {
...content.server,
middlewareMode: true,
https: httpsOptions,
},
},
});

const app = express();

// Create Rsbuild DevServer instance (middleware only)
const rsbuildServer = await rsbuild.createDevServer();

// Apply Rsbuild’s built-in middlewares
app.use(rsbuildServer.middlewares);

const httpsServer = https.createServer(httpsOptions, app);

httpsServer.listen(rsbuildServer.port, async () => {
// Notify Rsbuild that the custom server has started
await rsbuildServer.afterListen();
});

rsbuildServer.connectWebSocket({ server: httpsServer });

return {
close: async () => {
await rsbuildServer.close();
httpsServer.close();
},
};
}

startDevServer();
26 changes: 26 additions & 0 deletions rsbuild/express-https/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
body {
margin: 0;
color: #fff;
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
background-image: linear-gradient(to bottom, #020917, #101725);
}

.content {
display: flex;
min-height: 100vh;
line-height: 1.1;
text-align: center;
flex-direction: column;
justify-content: center;
}

.content h1 {
font-size: 3.6rem;
font-weight: 700;
}

.content p {
font-size: 1.2rem;
font-weight: 400;
opacity: 0.5;
}
8 changes: 8 additions & 0 deletions rsbuild/express-https/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import './index.css';

document.querySelector('#root').innerHTML = `
<div class="content">
<h1>Rsbuild with Express (HTTPS)</h1>
<p>Custom Express HTTPS server + Rsbuild middlewares and HMR.</p>
</div>
`;
Loading