-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (34 loc) · 1.17 KB
/
index.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
const express = require('express');
const app = express();
require('dotenv').config();
const mongoose = require('mongoose');
const fs = require('fs');
const bp = require('body-parser');
const PORT = process.env.PORT || 3551;
const DB_URI = process.env.DB_URI || "mongodb://localhost/Axis";
app.listen(PORT, async() => {
console.log("Axis Backend listening on port " + PORT);
console.log("Made with effort from @optixyt <3");
await connectDB();
require('./api/api.js');
});
app.use(express.json());
app.use(bp.urlencoded({ extended: true }));
fs.readdirSync("./routes").forEach(fileName => {
app.use(require(`./routes/${fileName}`));
});
async function connectDB() {
try {
await mongoose.connect(DB_URI)
console.log("Connected to MongoDB!")
} catch(err) {
console.log("Failed to connect to MongoDB: " + err)
}
}
app.get('/', (req, res) => {
res.json({ name: "Axis Backend", creator: "OptiX YT" });
});
app.use((req, res, next) => {
res.status(404).json({ errorCode: "com.axis.backend.route-undefined", errorMessage: "No route was found." });
console.log("Unknown route hit: [" + req.method + "] " + req.url);
});