fix build/dev env

This commit is contained in:
Dobromir Popov
2024-02-27 15:31:47 +02:00
parent b6abb81195
commit b1ea364663
4 changed files with 29 additions and 28 deletions

View File

@ -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

2
.vscode/launch.json vendored
View File

@ -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

View File

@ -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",

View File

@ -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}`)