From b1ea364663cf96cead292dff73f9d5c4d7c36845 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 27 Feb 2024 15:31:47 +0200 Subject: [PATCH] fix build/dev env --- .env.development | 4 ++-- .vscode/launch.json | 2 +- package.json | 2 +- server.js | 49 +++++++++++++++++++++++---------------------- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/.env.development b/.env.development index 7398df1..4e03747 100644 --- a/.env.development +++ b/.env.development @@ -1,10 +1,10 @@ NODE_TLS_REJECT_UNAUTHORIZED=0 # NODE_EXTRA_CA_CERTS=C:\\Users\\popov\\AppData\\Local\\mkcert -NEXT_PUBLIC_PROTOCOL=http +NEXT_PUBLIC_PROTOCOL=https NEXT_PUBLIC_HOST=localhost NEXT_PUBLIC_PORT=3003 -NEXTAUTH_URL=http://localhost:3003 +NEXTAUTH_URL=https://localhost:3003 SSL_ENABLED=true SSL_KEY=./certificates/localhost-key.pem diff --git a/.vscode/launch.json b/.vscode/launch.json index 1624b13..bd816c5 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -17,7 +17,7 @@ }, { "name": "Run npm nodemon (DEV)", - "command": "npm run debug-npm-env", // > _logs/debug.log + "command": "npm run debug-env", // > _logs/debug.log "request": "launch", "type": "node-terminal", "preLaunchTask": "killInspector", // <-- Add this line diff --git a/package.json b/package.json index 158ca44..2ddb8ae 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "homepage": "https://git.d-popov.com/popov/next-cart-app", "scripts": { "debug": "nodemon --inspect server.js", - "debug-npm-env": "dotenv -e .env.$NODE_ENV -- nodemon --inspect server.js", + "debug-env": "cross-env NODE_ENV=development dotenv -e .env.development -- nodemon --inspect server.js", "build": "next build", "buildWin": "npm run build", "start": "next start", diff --git a/server.js b/server.js index cb3b328..7ff5548 100644 --- a/server.js +++ b/server.js @@ -69,26 +69,6 @@ app .then(() => { const server = express(); - //check if ssl is enabled - if (process.env.SSL_ENABLED === "true") { - console.log("SSL_ENABLED = true"); - // Redirect from http to https - // server.use((req, res, next) => { - // if (req.headers['x-forwarded-proto'] !== 'https') { - // return res.redirect(`https://${req.headers.host}${req.url}`); - // } - // next(); - // }); - if (process.env.SSL_KEY && process.env.SSL_CERT) { - const options = { - key: fs.readFileSync(process.env.SSL_KEY), - cert: fs.readFileSync(process.env.SSL_CERT), - secureProtocol: 'TLSv1_2_method', // Example: Force TLS 1.2 - }; - https.createServer(options, server).listen(PORT); - } - } - // Add the middleware to set 'x-forwarded-host' header server.use((req, res, next) => { req.headers['x-forwarded-host'] = req.headers['x-forwarded-host'] || req.headers.host; @@ -569,10 +549,31 @@ app return handle(req, res); }); - server.listen(PORT, (err) => { - if (err) throw err; - console.log(`> Ready on ${PROTOCOL}://${HOST}:${PORT}`); - }); + //check if ssl is enabled + if (process.env.SSL_ENABLED === "true") { + console.log("SSL_ENABLED = true"); + // Redirect from http to https + // server.use((req, res, next) => { + // if (req.headers['x-forwarded-proto'] !== 'https') { + // return res.redirect(`https://${req.headers.host}${req.url}`); + // } + // next(); + // }); + if (process.env.SSL_KEY && process.env.SSL_CERT) { + const options = { + key: fs.readFileSync(process.env.SSL_KEY), + cert: fs.readFileSync(process.env.SSL_CERT), + secureProtocol: 'TLSv1_2_method', // Example: Force TLS 1.2 + }; + https.createServer(options, server).listen(PORT); + } + } + else { + server.listen(PORT, (err) => { + if (err) throw err; + console.log(`> Ready on ${PROTOCOL}://${HOST}:${PORT}`); + }); + } }) .catch((ex) => { console.warn(`Error starting server on ${HOST}:${PORT}`)