Skip to content

Commit

Permalink
πŸ“¦ NEW: You can now use a custom node server (#14)
Browse files Browse the repository at this point in the history
* πŸ“¦ NEW: You can now use a custom node server

* πŸ“– DOC: add example
  • Loading branch information
rphlmr authored Nov 5, 2024
1 parent 32c7cc0 commit 0ee11d2
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
32 changes: 31 additions & 1 deletion examples/remix-unstable-custom-build/app/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
import { createSecureServer } from "node:http2";
import mkcert from "mkcert";
import { createHonoServer } from "react-router-hono-server/node";

import { exampleMiddleware } from "./middleware";

const getSSLConfig = async () => {
const ca = await mkcert.createCA({
organization: "Development CA",
countryCode: "US",
state: "California",
locality: "San Francisco",
validity: 365,
});

const cert = await mkcert.createCert({
domains: ["127.0.0.1", "localhost"],
validity: 365,
ca: { key: ca.key, cert: ca.cert },
});

return { cert: cert.cert, key: cert.key };
};

const cert = await getSSLConfig();

export const server = await createHonoServer({
buildDirectory: "dist",
configure(server) {
server.use("*", exampleMiddleware());
},
listeningListener(info) {
console.log(`Server is listening on http://localhost:${info.port}`);
console.log(`Server is listening on https://localhost:${info.port}`);
},
customNodeServer: {
createServer: createSecureServer,
serverOptions: {
cert: cert.cert,
key: cert.key,
},
},
});
1 change: 1 addition & 0 deletions examples/remix-unstable-custom-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@remix-run/node": "^2.11.1",
"@remix-run/react": "^2.11.1",
"isbot": "^4.1.0",
"mkcert": "^3.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-hono-server": "*"
Expand Down
36 changes: 34 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-router-hono-server",
"version": "0.5.0",
"version": "1.0.0",
"description": "This package includes helper function to create an Hono app in your entry.server.tsx file. It allows you to customize your server.",
"exports": {
"./node": {
Expand Down
34 changes: 34 additions & 0 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import type { ServerOptions as ServerOptions$1, createServer } from "node:http";
import type {
SecureServerOptions,
ServerOptions as ServerOptions$3,
createSecureServer,
createServer as createServer$2,
} from "node:http2";
import type { ServerOptions as ServerOptions$2, createServer as createServer$1 } from "node:https";
import type { AddressInfo } from "node:net";
import path from "node:path";
import url from "node:url";
Expand All @@ -9,9 +17,28 @@ import type { HonoOptions } from "hono/hono-base";
import { logger } from "hono/logger";
import type { BlankEnv } from "hono/types";
import { type RemixMiddlewareOptions, remix } from "remix-hono/handler";

import { importDevBuild } from "./dev-build";
import { cache } from "./middleware";

type createHttpOptions = {
serverOptions?: ServerOptions$1;
createServer?: typeof createServer;
};
type createHttpsOptions = {
serverOptions?: ServerOptions$2;
createServer?: typeof createServer$1;
};
type createHttp2Options = {
serverOptions?: ServerOptions$3;
createServer?: typeof createServer$2;
};
type createSecureHttp2Options = {
serverOptions?: SecureServerOptions;
createServer?: typeof createSecureServer;
};
type CreateNodeServerOptions = createHttpOptions | createHttpsOptions | createHttp2Options | createSecureHttp2Options;

export type HonoServerOptions<E extends Env = BlankEnv> = {
/**
* Enable the default logger
Expand Down Expand Up @@ -86,6 +113,12 @@ export type HonoServerOptions<E extends Env = BlankEnv> = {
* {@link HonoOptions}
*/
honoOptions?: HonoOptions<E>;
/**
* Customize the node server (ex: using http2)
*
* {@link https://hono.dev/docs/getting-started/nodejs#http2}
*/
customNodeServer?: CreateNodeServerOptions;
};

const defaultOptions: HonoServerOptions<BlankEnv> = {
Expand Down Expand Up @@ -187,6 +220,7 @@ export async function createHonoServer<E extends Env = BlankEnv>(options: HonoSe
serve(
{
...server,
...mergedOptions.customNodeServer,
port: mergedOptions.port,
},
mergedOptions.listeningListener
Expand Down

0 comments on commit 0ee11d2

Please sign in to comment.