-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·112 lines (95 loc) · 3.09 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
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
107
108
109
110
111
112
const express = require('express');
const app = express();
const globalErrHandler = require('./controllers/errorController');
const AppError = require('./utils/appError');
const database = include('/models/databaseConnection');
// const session = require('express-session');
const session = require('cookie-session');
const cors = require('cors');
app.set('trust proxy', 1);
app.use(
cors({
origin: [
'http://localhost:3000',
'https://binibinapp.vercel.app',
'https://binibin-client.herokuapp.com',
'http://binibin-client.herokuapp.com',
],
optionsSuccessStatus: 200,
exposedHeaders: ['set-cookie'],
credentials: true,
})
);
// app.use(function (req, res, next) {
// res.setHeader('Access-Control-Allow-Origin', 'https://binibin.vercel.app');
// res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
// res.setHeader(
// 'Access-Control-Allow-Methods',
// 'GET, POST, OPTIONS, PUT, PATCH, DELETE'
// );
// res.setHeader(
// 'Access-Control-Allow-Headers',
// 'X-Requested-With,content-type'
// );
// res.setHeader('Access-Control-Allow-Credentials', true);
// next();
// });
// Testing ability to connect to db
database.connect((err, dbConnection) => {
if (!err) {
console.log('Successfully tested connection to PostgreSQL');
dbConnection.release();
console.log('Successfully disconnected test connection to PostgreSQL');
} else {
console.log('Error Connecting to PostgreSQL', err);
}
});
// Body parser, reading data from body into req.body
app.use(express.json());
// i think this is sessions
app.use(
session({
secret: 'secretSauce1234!',
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
secure: true,
maxAge: 24 * 60 * 60 * 1000,
sameSite: 'none',
},
})
);
// passport stuff
const passport = require('./middleware/passport');
app.use(passport.initialize());
// app.use(passport.session());
app.use((req, res, next) => {
console.log(`user details: ${JSON.stringify(req.user)}`);
console.log(`session object: ${JSON.stringify(req.session)}`);
console.log(
`session details passport ${JSON.stringify(req.session.passport)}`
);
next();
});
// Routes
const indexRouter = include('routes/index');
const usersRouter = include('routes/user');
const authRouter = include('routes/auth');
const entriesRouter = include('routes/entry');
// Routes
app.use('/auth', authRouter);
app.use('/api/users', usersRouter);
app.use('/api/entries', entriesRouter);
app.use('/api', (req, res, next) => {
res.send('binibin api');
});
app.use('/', indexRouter);
// handle undefined Routes
app.use('*', (req, res, next) => {
const err = new AppError(404, 'fail', 'undefined route');
next(err, req, res, next);
});
// Error handler
app.use(globalErrHandler);
module.exports = app;