-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
106 lines (86 loc) · 2.89 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict'
const express = require('express')
const path = require('path')
const cors = require('cors')
const app = express()
// mongoose and mongo connection
const { mongoose } = require("./db/mongoose");
mongoose.set('useFindAndModify', false);
// import the mongoose models
const { User } = require("./models/user");
const { Bike } = require("./models/bike");
// to validate object IDs
const { ObjectID } = require("mongodb");
// body-parser: middleware for parsing HTTP JSON body into a usable object
const bodyParser = require("body-parser");
app.use(bodyParser.json());
// express-session for managing user sessions
const session = require("express-session");
const MongoStore = require('connect-mongo')(session);
app.use(bodyParser.urlencoded({ extended: true }));
function isMongoError(error) { // checks for first error returned by promise rejection if Mongo database suddently disconnects
return typeof error === 'object' && error !== null && error.name === "MongoNetworkError"
}
// middleware for mongo connection error for routes that need it
const mongoChecker = (req, res, next) => {
// check mongoose connection established.
if (mongoose.connection.readyState != 1) {
log('Issue with mongoose connection')
res.status(500).send('Internal server error')
return;
} else {
next()
}
}
// Middleware for authentication of resources
const authenticate = (req, res, next) => {
if (req.session.user) {
User.findById(req.session.user).then((user) => {
if (!user) {
return Promise.reject()
} else {
req.user = user
next()
}
}).catch((error) => {
res.status(401).send("Unauthorized")
})
} else {
res.status(401).send("Unauthorized")
}
}
// app.use(cors({
// origin:['http://localhost:5000'],
// methods:['GET','POST'],
// credentials: true // enable set cookie
// }));
// Create a session and session cookie
app.use(
session({
secret: "our hardcoded secret",
resave: false,
saveUninitialized: false,
cookie: {
expires: 1000*60*60,
httpOnly: true
},
store: new MongoStore({ mongooseConnection: mongoose.connection}),
unset: 'destroy'
})
);
app.use(cors());
app.use(require('./routes/user'))
app.use(require('./routes/bike'))
app.use(express.static(path.join(__dirname, '/client/build')))
app.get('*', (req, res) => {
const goodRoutes = ['/', '/loggedIn', '/login', '/signup', '/admin',
'/postedads', '/results', '/postad', '/CompleteBikeInfo', '/User', '/rentedads']
if (!goodRoutes.includes(req.url)) {
res.status(404)
}
res.sendFile(path.join(__dirname, '/client/build/index.html'))
})
const port = process.env.PORT || 5000
app.listen(port, () => {
console.log(`listening on port ${port}...`)
})