ryu-js / startup /lifestyle.js
randydev's picture
Update startup/lifestyle.js
b45d8e6 verified
raw
history blame
1.41 kB
// startup like fastapi similar
import { Database } from '../database/database.js';
import {
UsernameDB,
PasswordDB
} from '../config.js';
import mongoose from 'mongoose';
const port = 7860
const startup = async () => {
try {
await mongoose.connect(`mongodb+srv://${UsernameDB}:${PasswordDB}@cluster0.1asx6h1.mongodb.net/AkenoXJs?retryWrites=true&w=majority`);
console.log("Mongoose connected successfully.");
const collection = mongoose.connection.collection("ApiKey");
try {
await collection.dropIndex("expiresAt_1");
console.log("Dropped TTL index on expiresAt.");
} catch (err) {
if (err.codeName === "IndexNotFound") {
console.log("No TTL index found, skipping dropIndex.");
} else {
console.error("Error dropping index:", err.message);
}
}
const dbClient = new Database("AkenoXJs");
console.log("Starting application...");
await dbClient.connect();
console.log("MongoDB connected successfully.");
} catch (error) {
console.error("Error during startup:", error.message);
process.exit(1);
}
};
const startServer = async (app) => {
await startup();
app.listen(port, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${port}`);
});
};
export { startServer };