-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
52 lines (42 loc) · 1.47 KB
/
app.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
52
const express = require('express');
const mongoose = require('mongoose');
const path = require("path")
const User = require('./models/user');
const Post = require('./models/post');
const app = express();
const { MONGOURI, PORT } = require('./config/keys');
/* mongoose.connect(MONGOURI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
mongoose.connection.on('connected', () => {
console.log("connection made to atlas")
})
mongoose.connect(MONGOURI)
mongoose.connection.on('error', () => {
console.log("ERROR: during connection to atlas")
}) */
//connecting to mongo atlas
mongoose.connect(MONGOURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then((result) => {
//app.listen(PORT)
console.log(`connected to atlas, port ${PORT}`)
})
.catch((err) => console.log(err));
//middleware
app.use(express.json())
app.use(express.urlencoded({ extended: true })); //passes all the urlencoded data and makes it accessible as an object
app.use('/', require('./routes/auth'))
app.use('/', require('./routes/posts'))
app.use('/', require('./routes/user'))
//build static files when serving in production
if (process.env.NODE_ENV == 'production') {
app.use(express.static(path.join(__dirname, "client", "build")))
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
}
//listen on specified port number for requests
app.listen(PORT, () => {
console.log(`Server is running on ${PORT}`)
})