From 02b3ef84f02e62f93f06c63d7a20df2fab1f76b4 Mon Sep 17 00:00:00 2001 From: Divlo Date: Sun, 22 Mar 2020 03:52:04 +0100 Subject: [PATCH] frontend: Custom server Next.js --- website/package-lock.json | 56 +++++++++++++++++++++++++++++++++++++++ website/package.json | 9 ++++--- website/server.js | 28 ++++++++++++++++++++ 3 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 website/server.js diff --git a/website/package-lock.json b/website/package-lock.json index 2c91bac..5e6a9ec 100644 --- a/website/package-lock.json +++ b/website/package-lock.json @@ -2698,6 +2698,15 @@ "sha.js": "^2.4.8" } }, + "cross-env": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.2.tgz", + "integrity": "sha512-KZP/bMEOJEDCkDQAyRhu3RL2ZO/SUVrxQVI0G3YEQ+OLbRA3c6zgixe8Mq8a/z7+HKlNEjo8oiLUs8iRijY2Rw==", + "dev": true, + "requires": { + "cross-spawn": "^7.0.1" + } + }, "cross-fetch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.0.4.tgz", @@ -2707,6 +2716,17 @@ "whatwg-fetch": "3.0.0" } }, + "cross-spawn": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.1.tgz", + "integrity": "sha512-u7v4o84SwFpD32Z8IIcPZ6z1/ie24O6RU3RbtL5Y316l3KuHVPx9ItBgWQ6VlfAFnRnTtMUrsQ9MUUTuEZjogg==", + "dev": true, + "requires": { + "path-key": "^3.1.0", + "shebang-command": "^2.0.0", + "which": "^2.0.1" + } + }, "crypto-browserify": { "version": "3.12.0", "resolved": "https://registry.npmjs.org/crypto-browserify/-/crypto-browserify-3.12.0.tgz", @@ -4048,6 +4068,12 @@ "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" }, + "isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", + "dev": true + }, "isobject": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz", @@ -5116,6 +5142,12 @@ "resolved": "https://registry.npmjs.org/path-is-inside/-/path-is-inside-1.0.2.tgz", "integrity": "sha1-NlQX3t5EQw0cEa9hAn+s8HS9/FM=" }, + "path-key": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", + "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==", + "dev": true + }, "path-parse": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", @@ -6348,6 +6380,21 @@ "kind-of": "^6.0.2" } }, + "shebang-command": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", + "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==", + "dev": true, + "requires": { + "shebang-regex": "^3.0.0" + } + }, + "shebang-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz", + "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", + "dev": true + }, "shell-quote": { "version": "1.7.2", "resolved": "https://registry.npmjs.org/shell-quote/-/shell-quote-1.7.2.tgz", @@ -7944,6 +7991,15 @@ "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz", "integrity": "sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q==" }, + "which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "dev": true, + "requires": { + "isexe": "^2.0.0" + } + }, "worker-farm": { "version": "1.7.0", "resolved": "https://registry.npmjs.org/worker-farm/-/worker-farm-1.7.0.tgz", diff --git a/website/package.json b/website/package.json index 85d0aeb..9bb10bf 100644 --- a/website/package.json +++ b/website/package.json @@ -2,10 +2,10 @@ "name": "functionproject-next", "version": "1.0.0", "scripts": { - "dev": "next dev", + "dev": "cross-env NODE_ENV=dev node server", "build": "next build", - "start": "next start", - "prod": "next export" + "export": "next export", + "start": "cross-env NODE_ENV=production node server" }, "dependencies": { "@zeit/next-css": "^1.0.1", @@ -15,5 +15,8 @@ "nprogress": "^0.2.0", "react": "16.13.0", "react-dom": "16.13.0" + }, + "devDependencies": { + "cross-env": "^7.0.2" } } diff --git a/website/server.js b/website/server.js new file mode 100644 index 0000000..d4372c3 --- /dev/null +++ b/website/server.js @@ -0,0 +1,28 @@ +const { createServer } = require("http"); +const { parse } = require("url"); +const next = require("next"); + +const PORT = process.env.PORT || 3000; +const dev = process.env.NODE_ENV !== "production"; +const app = next({ dev }); +const handle = app.getRequestHandler(); + +app.prepare().then(() => { + createServer((req, res) => { + // Be sure to pass `true` as the second argument to `url.parse`. + // This tells it to parse the query portion of the URL. + const parsedUrl = parse(req.url, true); + const { pathname, query } = parsedUrl; + + if (pathname === "/a") { + app.render(req, res, "/b", query); + } else if (pathname === "/b") { + app.render(req, res, "/a", query); + } else { + handle(req, res, parsedUrl); + } + }).listen(PORT, (err) => { + if (err) throw err; + console.log(`> Ready on http://localhost:${PORT}`); + }); +}); \ No newline at end of file