Skip to content

Commit

Permalink
Fix mongodb VPS connection
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmorais committed Sep 8, 2024
1 parent 763b6c4 commit 5f7eb49
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ yarn-error.*

# local env files
.env*.local
.env

# typescript
*.tsbuildinfo
43 changes: 31 additions & 12 deletions backend/src/databases/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,37 @@ import mongoose from "mongoose";
import dotenv from 'dotenv'
dotenv.config();
const user = process.env.MG_USER
const password = process.env.MG_PASS
const db = process.env.MG_DB
const password = process.env.MG_PASS
const db = process.env.MG_DB
const myHost = process.env.HOST
const connectToDatabase = async () => {
try {
await mongoose.connect(`mongodb://${user}:${password}@${myHost}:27017/${db}`);
console.log("Conectado ao MongoDB");
} catch (error) {
console.error("Erro ao conectar ao MongoDB", error);
process.exit(1); // Sair do processo em caso de falha na conexão
}
};

export default connectToDatabase;

const uri = `mongodb://${user}:${password}@${myHost}/${db}`;
const uriLocal = "mongodb://localhost:27017/dnutri"

export default function connectToDatabase() {
mongoose.connection.on("connected", () => console.log("Mongo conectado com sucesso!"));
mongoose.connection.on("disconnected", () => console.log("Mongo Desconectado com sucesso!"));


mongoose.connect(uriLocal, {
serverSelectionTimeoutMS: 5000,
maxPoolSize: 10,
serverApi: { version: '1', strict: true, deprecationErrors: true }
})
.catch((e) => {
console.error("Connection Failed", e.message)
})

process.on("SIGINT", async () => {
try {
console.log("Connection closed");
await mongoose.connection.close();
process.exit(0);
}
catch (error) {
console.error("Problems to close the connection", error);
process.exit(1);
}
})
}
4 changes: 2 additions & 2 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import createPostgresTables from "./models/postgres";
import dotenv from "dotenv";
import router from "./routes/userRoutes";
import CreateMongoDbCollections from "./models/mongo";
import insertUser from "./controllers/createController";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3002;

app.use(express.json());

//createPostgresTables();
createPostgresTables();
CreateMongoDbCollections();
//insertUser();

app.use(router)
app.listen(PORT, () => {
console.log(`Servidor rodando na porta ${PORT}`);
});
4 changes: 2 additions & 2 deletions backend/src/models/mongo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import connectToDatabase from '../../databases/mongo'
export default async function CreateMongoDbCollections() {
try {
connectToDatabase();
/* await Data.createCollection()
await User.createCollection() */
await Data.createCollection()
await User.createCollection()

} catch (error: any) {
console.error("Erro ao criar collections no MongoDB:", error.message)
Expand Down

0 comments on commit 5f7eb49

Please sign in to comment.