-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.js
51 lines (40 loc) · 1.68 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
50
51
//=============================
// Dependencies
//=============================
const express = require("express");
const mongoose = require("mongoose");
//=============================
// Environment Variables
//=============================
const app = express();
const mongoURI =
process.env.MONGODB_URI || "mongodb://localhost:27017/merncrud";
const PORT = process.env.PORT || 3000;
//=============================
// Middleware
//=============================
app.use(express.urlencoded({ extended: false })); // extended: false - does not allow nested objects in query strings
app.use(express.json()); //use .json(), not .urlencoded()
app.use(express.static("public")); // we need to tell express to use the public directory for static files... this way our app will find index.html as the route of the application! We can then attach React to that file!
//=============================
// MongoDB Connection
//=============================
mongoose.connect(mongoURI, { useNewUrlParser: true }, () => {
console.log("Established Connection with mongo", mongoURI);
});
//======================
// DB Messaging
//======================
mongoose.connection.on("error", (err) => console.log(err.message));
mongoose.connection.on("disconnected", () => console.log("mongo disconnected"));
//=============================
// Routers
//=============================
const todosController = require("./controllers/todos");
app.use("/todos", todosController);
//=================================================
// Listening on Port 3000 or Default to HEROKU
//=================================================
app.listen(PORT, () => {
console.log(`Ascoltando al porto... ${PORT}`);
});