-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (53 loc) · 1.47 KB
/
server.js
File metadata and controls
57 lines (53 loc) · 1.47 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
const mongoose = require("mongoose");
const express = require("express");
const app = express();
const router = express.Router();
const db = require("./config/keys").mongoURI;
const tokenRouter = require("./routes/tokenRouter");
const schoolsRouter = require("./routes/schoolsRouter");
const logger = require("./middlewares/log");
const swaggerUi = require("swagger-ui-express");
const swaggerDocument = require("./swagger/swagger.json");
const cssOptions = require("./swagger/cssOptions");
const path = require("path");
const PORT = process.env.PORT || 3000;
mongoose
.connect(db, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("Connected to MongoDB");
})
.catch((err) => {
console.log("Error connecting to MongoDB", err);
logger.log({
level: "error",
message: err.message,
timestamp: new Date(),
});
});
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
// load index.html on root from static folder
app.use(express.static(path.join(__dirname, "static")));
app.use("/api/token", tokenRouter);
app.use("/api/schools", schoolsRouter);
app.use(
"/api-docs",
swaggerUi.serve,
swaggerUi.setup(swaggerDocument, cssOptions)
);
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({
message: err.message,
});
});