-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
71 lines (56 loc) · 1.7 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
const express = require('express')
const app = express()
const mongoose = require('mongoose')
const cors = require('cors')
const PORT = process.env.PORT || 3003
const mongo_uri = process.env.MONGODB_URI || 'mongodb://localhost:27017/project_three'
const session = require('express-session')
//middleware
app.use(express.json());
const whitelist = ['http://localhost:3000']
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.includes(origin)) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
}
}
app.use(cors())
app.use(
session({
secret: 'pop',
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
maxAge: 3600000
}
})
)
// app.use((req,res, next) => {
// console.log(req.session);
// next()
// })
//mongoose
mongoose.connection.on('error', err => console.log(err.message + ' THIS IS THE ERROR MESSAGE'))
mongoose.connection.on('disconect', () => console.log('mongo has disconnected'))
mongoose.connect(mongo_uri, {useNewUrlParser: true, useUnifiedTopology: true})
mongoose.connection.once('open', () => {
console.log('connected to mongoose')
})
//controller
const cocktailController = require('./controllers/cocktail.js')
app.use('/cocktail', cocktailController)
const userController = require('./controllers/users_controller.js')
app.use('/user', userController)
const sessionController = require('./controllers/sessions_controller.js')
app.use('/session', sessionController)
app.get('/', (req,res)=> {
res.send('hello')
})
//listening port
app.listen(PORT, ()=> {
console.log('listening on port ', PORT)
})