-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
53 lines (42 loc) · 1.7 KB
/
app.js
File metadata and controls
53 lines (42 loc) · 1.7 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
import { config } from 'dotenv';
import mongoose from 'mongoose';
import express from 'express';
import { middlewareConfig } from './config/index.js';
import { errorHandler } from './error-handling/errorHandler.js';
import indexRoutes from './routes/index.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/myorigin-app-server";
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);
});
// ℹ️ 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);
// ❗ 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;