-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (43 loc) · 1.88 KB
/
server.js
File metadata and controls
50 lines (43 loc) · 1.88 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
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const path = require("path");
require("dotenv").config(); //environment variables in the .env file
const app = express(); //express server
const port = process.env.PORT || 5000; //port for server
app.use(cors()); //middleware Cross origin resource sharing
app.use(express.json()); //middleware to parse json
//DB
const uri = process.env.ATLAS_URI; //ATLAS_URI is the env variable. set it
mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
}); //uri is where DB is stored
const connection = mongoose.connection;
connection.once("open", () => {
console.log("MongoDB connection established successfully");
});
//routes
const ordersRouter = require("./routes/orders");
const categoriesRouter = require("./routes/categories");
const photographersRouter = require("./routes/photographers");
const registration_photographerRouter = require("./routes/registration_photographer");
const usersRouter = require("./routes/users");
app.use("/orders", ordersRouter); //it will load everything in the user
app.use("/categories", categoriesRouter); //it will load everything in the categories
app.use("/photographers", photographersRouter); //it will load everything in the photographers
app.use("/registration_photographer", registration_photographerRouter); //it will load everything in the registration_photographer
app.use("/uploads", express.static("uploads"));
app.use("/users", usersRouter);
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
//Listen
app.listen(port, () => {
//server is listening on the port
console.log(`Server is running on port: ${port}`);
});