Add dotenv support and update server config

Introduced dotenv for environment variable management and created a .env file for port configuration. Updated server to use HOST and PORT from environment, allowing access from all IPs. Modified Socket.io client connection to use window.location.origin for remote server compatibility.
This commit is contained in:
apiboomer
2025-08-27 01:48:09 +03:00
parent e60082938f
commit e56c743851
5 changed files with 28 additions and 3 deletions

1
.env Normal file
View File

@@ -0,0 +1 @@
PORT=7010

18
package-lock.json generated
View File

@@ -10,6 +10,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"bad-words": "^3.0.4", "bad-words": "^3.0.4",
"dotenv": "^16.6.1",
"express": "^4.18.2", "express": "^4.18.2",
"moment": "^2.29.4", "moment": "^2.29.4",
"socket.io": "^4.7.2" "socket.io": "^4.7.2"
@@ -300,6 +301,18 @@
"npm": "1.2.8000 || >= 1.4.16" "npm": "1.2.8000 || >= 1.4.16"
} }
}, },
"node_modules/dotenv": {
"version": "16.6.1",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.6.1.tgz",
"integrity": "sha512-uBq4egWHTcTt33a72vpSG0z3HnPuIl6NqYcTrKEg2azoEyl2hpW0zqlxysq2pK9HlDIHyHyakeYaYnSAwd8bow==",
"license": "BSD-2-Clause",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://dotenvx.com"
}
},
"node_modules/ee-first": { "node_modules/ee-first": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
@@ -1509,6 +1522,11 @@
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz",
"integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==" "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg=="
}, },
"dotenv": {
"version": "16.6.1",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.6.1.tgz",
"integrity": "sha512-uBq4egWHTcTt33a72vpSG0z3HnPuIl6NqYcTrKEg2azoEyl2hpW0zqlxysq2pK9HlDIHyHyakeYaYnSAwd8bow=="
},
"ee-first": { "ee-first": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",

View File

@@ -12,6 +12,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"bad-words": "^3.0.4", "bad-words": "^3.0.4",
"dotenv": "^16.3.1",
"express": "^4.18.2", "express": "^4.18.2",
"moment": "^2.29.4", "moment": "^2.29.4",
"socket.io": "^4.7.2" "socket.io": "^4.7.2"

View File

@@ -1,4 +1,5 @@
const socket = io(); // Socket.io bağlantısı - uzak sunucu için
const socket = io(window.location.origin);
// Elements // Elements
const $messageForm = document.querySelector("#message-form"); const $messageForm = document.querySelector("#message-form");

View File

@@ -1,3 +1,6 @@
// Load environment variables
require('dotenv').config();
// emit events // emit events
// socket.emit, io.emit, socket.broadcast.emit // socket.emit, io.emit, socket.broadcast.emit
@@ -29,6 +32,7 @@ const server = http.createServer(app);
const io = socketio(server); const io = socketio(server);
const port = process.env.PORT || 3000; const port = process.env.PORT || 3000;
const host = process.env.HOST || '0.0.0.0'; // Tüm IP'lerden erişime izin ver
// define paths for express config // define paths for express config
const publicDirectoryPath = path.join(__dirname, "../public"); const publicDirectoryPath = path.join(__dirname, "../public");
@@ -129,6 +133,6 @@ io.on("connection", (socket) => {
}); });
// start the server // start the server
server.listen(port, () => { server.listen(port, host, () => {
console.log(`Server is up on port ${port}!`); console.log(`Server is up on ${host}:${port}!`);
}); });