diff --git a/.gitignore b/.gitignore index b512c09..9c9857d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -node_modules \ No newline at end of file +node_modules +.env +src/logs/console.log diff --git a/config.js b/config.js new file mode 100644 index 0000000..aa862a2 --- /dev/null +++ b/config.js @@ -0,0 +1,7 @@ +require("dotenv").config(); + +module.exports = { + PORT: 3000, + MONGO_URI: process.env.MONGO_URI, + SESSION_SECRET: process.env.SESSION_SECRET +}; diff --git a/config.json b/config.json deleted file mode 100644 index 3558704..0000000 --- a/config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "sessionSecret" : "c1&5Id!M87d3", - "port": "3000" -} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 6d51261..f6c3582 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@types/node": "^18.0.6", "bcrypt": "^5.1.0", + "dotenv": "^16.1.3", "ejs": "^3.1.9", "express": "^4.18.2", "express-session": "^1.17.3", @@ -1699,6 +1700,17 @@ "node": ">=8" } }, + "node_modules/dotenv": { + "version": "16.1.3", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.1.3.tgz", + "integrity": "sha512-FYssxsmCTtKL72fGBSvb1K9dRz0/VZeWqFme/vSb7r7323x4CRaHu4LvQ5JG3+s6yt2YPbBrkpiEODktfyjI9A==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/motdotla/dotenv?sponsor=1" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -3043,9 +3055,9 @@ } }, "node_modules/socket.io-parser": { - "version": "4.2.2", - "resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.2.tgz", - "integrity": "sha512-DJtziuKypFkMMHCm2uIshOYC7QaylbtzQwiMYDuCKy3OPkjLzu4B2vAhTlqipRHHzrI0NJeBAizTK7X+6m1jVw==", + "version": "4.2.4", + "resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.4.tgz", + "integrity": "sha512-/GbIKmo8ioc+NIWIhwdecY0ge+qVBSMdgxGygevmdHj24bsfgtCmcUUcQ5ZzcylGFHsN3k4HB4Cgkl96KVnuew==", "dependencies": { "@socket.io/component-emitter": "~3.1.0", "debug": "~4.3.1" @@ -4753,6 +4765,11 @@ "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.1.tgz", "integrity": "sha512-463v3ZeIrcWtdgIg6vI6XUncguvr2TnGl4SzDXinkt9mSLpBJKXT3mW6xT3VQdDN11+WVs29pgvivTc4Lp8v+w==" }, + "dotenv": { + "version": "16.1.3", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.1.3.tgz", + "integrity": "sha512-FYssxsmCTtKL72fGBSvb1K9dRz0/VZeWqFme/vSb7r7323x4CRaHu4LvQ5JG3+s6yt2YPbBrkpiEODktfyjI9A==" + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -5745,9 +5762,9 @@ } }, "socket.io-parser": { - "version": "4.2.2", - "resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.2.tgz", - "integrity": "sha512-DJtziuKypFkMMHCm2uIshOYC7QaylbtzQwiMYDuCKy3OPkjLzu4B2vAhTlqipRHHzrI0NJeBAizTK7X+6m1jVw==", + "version": "4.2.4", + "resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.4.tgz", + "integrity": "sha512-/GbIKmo8ioc+NIWIhwdecY0ge+qVBSMdgxGygevmdHj24bsfgtCmcUUcQ5ZzcylGFHsN3k4HB4Cgkl96KVnuew==", "requires": { "@socket.io/component-emitter": "~3.1.0", "debug": "~4.3.1" diff --git a/package.json b/package.json index cb203ff..99b196a 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dependencies": { "@types/node": "^18.0.6", "bcrypt": "^5.1.0", + "dotenv": "^16.1.3", "ejs": "^3.1.9", "express": "^4.18.2", "express-session": "^1.17.3", diff --git a/src/db.js b/src/db.js index 95541a6..da3b27b 100644 --- a/src/db.js +++ b/src/db.js @@ -1,6 +1,7 @@ +const config = require("../config.js") const mongoose = require("mongoose"); const ObjectId = mongoose.Types.ObjectId; -const uri = "mongodb+srv://Mencoreh:RRyKUSz33lqXgkGm@cluster0.emhsybu.mongodb.net/ChatApp?retryWrites=true&w=majority"; +const uri = config.MONGO_URI; //CONEXIÓN A MONGODB mongoose .connect(uri, { diff --git a/src/index.js b/src/index.js index 931e9db..4fabba9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,5 @@ // Inicializar el servidor express y requerir dependencias -const config = require("../config.json") +const config = require("../config.js") const express = require("express"); const app = express(); const path = require("path"); @@ -12,7 +12,7 @@ const SocketIO = require("socket.io"); // Usar express-sesion para manejar las sesiones const sessionMiddleware = session({ - secret: config.sessionSecret, + secret: config.SESSION_SECRET, resave: false, saveUninitialized: true, }); @@ -32,8 +32,8 @@ app.set("view engine", "ejs"); app.set("views", path.join(__dirname, "views")); // Inicializar el servidor y establecer Socket.IO -const server = app.listen(config.port, "0.0.0.0", () => { - console.log(`Servidor Express funcionando en http://0.0.0.0:${config.port}`); +const server = app.listen(config.PORT, "0.0.0.0", () => { + console.log(`Servidor Express funcionando en http://0.0.0.0:${config.PORT}`); }); const io = SocketIO(server);