forked from baileymbeck/Nasa-Kids
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
82 lines (65 loc) · 2.18 KB
/
server.js
File metadata and controls
82 lines (65 loc) · 2.18 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const express = require("express");
const path = require("path");
const mongoose = require("mongoose");
const routes = require("./routes");
const bodyParser = require('body-parser')
const morgan = require('morgan')
const session = require('express-session')
const MongoStore = require('connect-mongo')(session)
const dbConnection = require('./Server/db') // loads our connection to the mongo database
const passport = require('./Server/passport')
const app = express();
const PORT = process.env.PORT || 3001;
// Define middleware here
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// ===== Middleware ====
app.use(morgan('dev'))
app.use(
bodyParser.urlencoded({
extended: false
})
)
app.use(bodyParser.json())
app.use(
session({
secret: process.env.APP_SECRET || 'this is the default passphrase',
store: new MongoStore({ mongooseConnection: dbConnection }),
resave: false,
saveUninitialized: false
})
)
// ===== Passport ====
app.use(passport.initialize())
app.use(passport.session()) // will call the deserializeUser
// // Serve up static assets (usually on heroku)
// if (process.env.NODE_ENV === "production") {
// app.use(express.static("client/build"));
// }
/* Express app ROUTING */
app.use('/auth', require('./Server/auth'))
// ====== Error handler ====
app.use(function(err, req, res, next) {
console.log('====== ERROR =======')
console.error(err.stack)
res.status(500)
})
// Add routes, both API and view
app.use(routes);
// Connect to the Mongo DB
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/nasaUser");
// If the request does not match any other route, serve the matching file out
// of the build directory
if (process.env.NODE_ENV === 'production') {
app.use('/static', express.static(path.join(__dirname, 'client', 'build', 'static')));
app.use('/img', express.static(path.join(__dirname, 'client', 'build', 'img')));
app.use('/', (_, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'));
});
} else {
app.use('/', express.static(path.join(__dirname, 'client', 'public')))
}
// Start the API server
app.listen(PORT, function () {
console.log(`🌎 ==> API Server now listening on PORT ${PORT}!`);
});