-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
91 lines (73 loc) · 2.75 KB
/
app.js
File metadata and controls
91 lines (73 loc) · 2.75 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { config } from 'dotenv';
import mongoose from 'mongoose';
import express from 'express';
import swaggerUi from 'swagger-ui-express';
import swaggerJsdoc from 'swagger-jsdoc';
import fs from 'fs';
import YAML from 'yaml';
import { middlewareConfig } from './config/index.js';
import { errorHandler } from './middleware/errorHandler.js';
import indexRoutes from './routes/index.routes.js';
import authRoutes from './routes/auth.routes.js';
import familyRoutes from './routes/family.routes.js';
import memoryRoutes from './routes/memory.routes.js';
// ℹ️ Gets access to environment variables/settings
// https://www.npmjs.com/package/dotenv
config();
// ℹ️ --- DB ---
// Connects to the database
// package responsible to make the connection with mongodb
// https://www.npmjs.com/package/mongoose
// Sets the MongoDB URI for our app to have access to it.
// If no env has been set, we dynamically set it to whatever the folder name was upon the creation of the app
const MONGO_URI = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/memories';
mongoose
.connect(MONGO_URI)
.then((x) => {
const dbName = x.connections[0].name;
console.log(`Connected to Mongo! Database name: "${dbName}"`);
})
.catch((err) => {
console.error('Error connecting to mongo: ', err);
});
// ℹ️ --- Swagger docs ---
// Load the Swagger specifications from the YAML files
const memorySpec = YAML.parse(fs.readFileSync('./swagger/memory.yaml', 'utf8'));
const testSpec = YAML.parse(fs.readFileSync('./swagger/test.yaml', 'utf8'));
// Combine the specifications
const combinedSpec = {
...memorySpec,
...testSpec,
paths: {
...memorySpec.paths,
...testSpec.paths,
},
// Add more specifications if needed
};
// Swagger options
const options = {
swaggerDefinition: combinedSpec,
apis: ['./routes/*.js'], // Add your API files here
};
// Initialize swagger-jsdoc
const swaggerSpecs = swaggerJsdoc(options);
// ℹ️ Handles http requests (express is node js framework)
// https://www.npmjs.com/package/express
const app = express();
// ℹ️ This function is getting exported from the config folder. It runs most pieces of middleware
// (logger, body-parser etc)
middlewareConfig(app);
// ℹ️ --- ROUTES ---
app.use('/api', indexRoutes);
app.use('/auth', authRoutes);
app.use('/api', familyRoutes);
app.use('/api', memoryRoutes);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpecs));
// ❗ To handle errors. Routes that don't exist or errors that you handle in specific routes
errorHandler(app);
// Sets the PORT for our app to have access to it. If no env has been set, we hard code it to 5005
const PORT = process.env.PORT || 5005;
app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});
export default app;