-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
40 lines (34 loc) · 1.11 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
const express = require("express");
const app = express();
app.use(express.json());
const mongoose = require("mongoose");
const cors = require("cors");
const config = require("./config");
mongoose.connect(config.db.uri);
mongoose.connection.once("open", async () => {
console.log("Established connection to MongoDB.");
});
app.use(cors());
// Routes
app.use("/api/dogs", require("./routes/dogs"));
app.use("/api/application", require("./routes/application"));
app.use("/api/interviews", require("./routes/interviews"));
app.use("/api/admins", require("./routes/admins"));
app.use("/api/users", require("./routes/users"));
app.use("/api/contact", require("./routes/contact"));
// Error handling
app.use((err, req, res, _) => {
if (err.status === 500 || err.status == null) {
console.error(err);
return res.status(500).json({
message: `Something went wrong on the server: ${err.message}`,
});
}
// Catch ServiceError
return res.status(err.status).json({
message: err.message,
});
});
app.listen(config.app.port, () => {
console.log(`Server listening at http://localhost:${config.app.port}`);
});