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:
18
package-lock.json
generated
18
package-lock.json
generated
@@ -10,6 +10,7 @@
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"bad-words": "^3.0.4",
|
||||
"dotenv": "^16.6.1",
|
||||
"express": "^4.18.2",
|
||||
"moment": "^2.29.4",
|
||||
"socket.io": "^4.7.2"
|
||||
@@ -300,6 +301,18 @@
|
||||
"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": {
|
||||
"version": "1.1.1",
|
||||
"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",
|
||||
"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": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"bad-words": "^3.0.4",
|
||||
"dotenv": "^16.3.1",
|
||||
"express": "^4.18.2",
|
||||
"moment": "^2.29.4",
|
||||
"socket.io": "^4.7.2"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const socket = io();
|
||||
// Socket.io bağlantısı - uzak sunucu için
|
||||
const socket = io(window.location.origin);
|
||||
|
||||
// Elements
|
||||
const $messageForm = document.querySelector("#message-form");
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Load environment variables
|
||||
require('dotenv').config();
|
||||
|
||||
// emit events
|
||||
// socket.emit, io.emit, socket.broadcast.emit
|
||||
|
||||
@@ -29,6 +32,7 @@ const server = http.createServer(app);
|
||||
const io = socketio(server);
|
||||
|
||||
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
|
||||
const publicDirectoryPath = path.join(__dirname, "../public");
|
||||
|
||||
@@ -129,6 +133,6 @@ io.on("connection", (socket) => {
|
||||
});
|
||||
|
||||
// start the server
|
||||
server.listen(port, () => {
|
||||
console.log(`Server is up on port ${port}!`);
|
||||
server.listen(port, host, () => {
|
||||
console.log(`Server is up on ${host}:${port}!`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user