-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
28 lines (24 loc) · 936 Bytes
/
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
// IMPORTS
require("dotenv").config();
const path = require("path");
const cors = require("cors");
const express = require("express");
const mongoose = require("mongoose");
const apiRoutes = require("./routes");
const app = express();
const PORT = process.env.PORT || 3000;
const DB_URI = process.env.DB_URI;
// DATABASE CONNECTION
mongoose
.connect(DB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then((db) => console.log("DB connection success"))
.catch((error) =>
console.log("Error establishing a DB connection: " + error)
);
// MIDDLEWARE
app.use(cors()); // CORS Support
app.use(express.json()); // Parse request with JSON payloads
app.use("/api", apiRoutes); // Router, begins with "/api"
app.use(express.static(path.join(__dirname, "public"))); // Static files. Must be an public/index.html file to see response
// SERVER START
app.listen(PORT, () => console.log("Server started at port " + PORT));