diff --git a/starters/adapters/node-server/README.md b/starters/adapters/node-server/README.md new file mode 100644 index 00000000000..45b0a346237 --- /dev/null +++ b/starters/adapters/node-server/README.md @@ -0,0 +1,12 @@ +## Node Server + +This app has a minimal zero-dependencies server. Using the built-in `http.createServer` API. +This should be faster and less overhead than Express or other frameworks. + +After running a full build, you can preview the build using the command: + +``` +npm run serve +``` + +Then visit [http://localhost:8080/](http://localhost:8080/) diff --git a/starters/adapters/node-server/adapters/node-server/vite.config.ts b/starters/adapters/node-server/adapters/node-server/vite.config.ts new file mode 100644 index 00000000000..e744903408f --- /dev/null +++ b/starters/adapters/node-server/adapters/node-server/vite.config.ts @@ -0,0 +1,15 @@ +import { nodeServerAdapter } from "@builder.io/qwik-city/adapters/node-server/vite"; +import { extendConfig } from "@builder.io/qwik-city/vite"; +import baseConfig from "../../vite.config"; + +export default extendConfig(baseConfig, () => { + return { + build: { + ssr: true, + rollupOptions: { + input: ["src/entry.node-server.tsx", "@qwik-city-plan"], + }, + }, + plugins: [nodeServerAdapter({ name: "node-server" })], + }; +}); diff --git a/starters/adapters/node-server/package.json b/starters/adapters/node-server/package.json new file mode 100644 index 00000000000..1336051c279 --- /dev/null +++ b/starters/adapters/node-server/package.json @@ -0,0 +1,23 @@ +{ + "description": "Vanilla Node server", + "scripts": { + "build.server": "vite build -c adapters/node-server/vite.config.ts", + "serve": "node server/entry.node-server" + }, + "__qwik__": { + "priority": 19, + "displayName": "Adapter: Node.js Server", + "docs": [ + "https://qwik.builder.io/deployments/node/" + ], + "nextSteps": { + "title": "Next Steps", + "lines": [ + " Now you can build a production-ready node app:", + "", + " - pnpm run build: production build", + " - pnpm run serve: runs the production server locally" + ] + } + } +} diff --git a/starters/adapters/node-server/src/entry.node-server.tsx b/starters/adapters/node-server/src/entry.node-server.tsx new file mode 100644 index 00000000000..242faec314a --- /dev/null +++ b/starters/adapters/node-server/src/entry.node-server.tsx @@ -0,0 +1,36 @@ +/* + * WHAT IS THIS FILE? + * + * It's the entry point for the Express HTTP server when building for production. + * + * Learn more about Node.js server integrations here: + * - https://qwik.builder.io/docs/deployments/node/ + * + */ +import { createQwikCity } from "@builder.io/qwik-city/middleware/node"; +import qwikCityPlan from "@qwik-city-plan"; +import render from "./entry.ssr"; +import { manifest } from "@qwik-client-manifest"; +import { createServer } from "node:http"; + +// Allow for dynamic port +const PORT = process.env.PORT ?? 3004; + +// Create the Qwik City express middleware +const { router, notFound, staticFile } = createQwikCity({ + render, + qwikCityPlan, + manifest, +}); + +const server = createServer(); + +server.on("request", (req, res) => { + staticFile(req, res, () => { + router(req, res, () => { + notFound(req, res, () => {}); + }); + }); +}); + +server.listen(PORT);