-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
49 lines (43 loc) · 1.06 KB
/
server.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
49
const express = require("express");
const helmet = require("helmet");
const mongoose = require("mongoose");
const cors = require("cors");
const apiRouter = require("./routes");
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(helmet());
app.use(express.json({ limit: "50mb" }));
// app.use(upload.array());
app.use("/api", apiRouter);
const connectMongoDb = async () => {
try {
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("Connected to database");
} catch (err) {
throw new Error(err);
}
};
connectMongoDb();
// for ping ;)
app.get("/ping", (req, res) => {
res.send("success!");
});
// global error handeler
app.use((err, req, res, next) => {
if (err.message) {
return res.status(err.statusCode || 500).json({
error: err.message,
});
} else {
return res.status(500).json({
error: "Something Went Wrong",
});
}
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});