forked from thinkful-ei24/joe-stud-poker-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
69 lines (59 loc) · 1.53 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
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const mongoose = require('mongoose');
const passport = require('passport');
const {PORT ,DATABASE_URL} = require ('./config');
//Strategies
const {localStrategy, jwtStrategy} = require('./auth/user.strategy');
//Routers
const userRouter = require('./routes/user.route');
const authRouter = require('./routes/auth.route');
const app = express();
let server;
//Passport verification
passport.use(localStrategy); //Use localStrategy when logging in
passport.use(jwtStrategy); //Use jwtStrategy when receiving JWTs
//MIDDLEWARE
app.use(express.static('../public'))
app.use(express.json());
app.use(morgan('dev'));
app.use(cors());
//Router Mounting
app.use('/api/users', userRouter);
app.use('/api/auth', authRouter);
//*** */
//START SERVER
function startServer() {
return new Promise((resolve, reject) => {
mongoose.connect(DATABASE_URL, {useNewUrlParser: true}, err => {
if(err) {
reject(err);
}
server = app.listen(PORT, () => {
console.log(`express listening on ${PORT}`)
})
})
})
}
//STOP SERVER
function stopServer() {
return mongoose.disconnect()
.then(() => {
return new Promise((resolve, reject) => {
server.close(err => {
if(err) {
return reject(err);
}
console.log('Server killed');
resolve();
})
})
})
}
startServer().catch(err => console.log(err));
module.exports = {
startServer,
stopServer,
}