-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (39 loc) · 1.38 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const fastify = require('fastify');
const mongoose = require('mongoose');
const env = require('dotenv');
// Load environment variables
env.config();
// Create Fastify instance
const app = fastify();
// Load routes
const authRoute = require('./routes/authRoutes');
const userRoute = require('./routes/userRoutes');
const transactionsRoute = require('./routes/transactionRoutes');
// MongoDB connection
const MONGO_URL = process.env.MONGO_URL;
if (!MONGO_URL) {
throw new Error("MongoDB URL is not defined in the environment variables.");
}
// Connect to MongoDB
mongoose.connect(MONGO_URL, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('MongoDB connected successfully');
})
.catch(err => {
console.error('MongoDB connection error:', err);
});
// Register Routes
app.register(authRoute, { prefix: '/auth' });
app.register(userRoute, { prefix: '/user' });
app.register(transactionsRoute, { prefix: '/transactions' });
// Start server
const PORT = process.env.PORT || 3000;
const HOST = '0.0.0.0'; // Change this to listen on all available network interfaces
// Use object to specify listen options (host and port)
app.listen({ port: PORT, host: HOST }, (err, address) => {
if (err) {
console.error('Error starting the server:', err);
process.exit(1);
}
console.log(`Server is running on ${address}`);
});