-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
71 lines (61 loc) · 1.97 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
require('dotenv').config();
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const passport = require('passport');
const dataRoute = require('./routes/tripData.js');
const { router: usersRouter } = require('./Users');
const { router: authRouter, basicStrategy, jwtStrategy } = require('./Auth');
mongoose.Promise = global.Promise;
const { PORT, DATABASE_URL } = require('./config');
app.use(morgan('common'));
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(passport.initialize());
passport.use(basicStrategy);
passport.use(jwtStrategy);
app.use('/data', dataRoute);
app.use('/users', usersRouter);
app.use('/auth', authRouter);
function runServer(databaseUrl = DATABASE_URL, port = PORT) {
return new Promise((resolve, reject) => {
mongoose.connect(databaseUrl, err => {
if (err) {
return reject(err);
}
server = app
.listen(port, () => {
console.log(`Your app is listening on port ${port}`);
resolve();
})
.on('error', err => {
mongoose.disconnect();
reject(err);
});
});
});
}
// this function closes the server, and returns a promise. we'll
// use it in our integration tests later.
function closeServer() {
return mongoose.disconnect().then(() => {
return new Promise((resolve, reject) => {
console.log('Closing server');
server.close(err => {
if (err) {
return reject(err);
}
resolve();
});
});
});
}
// if server.js is called directly (aka, with `node server.js`), this block
// runs. but we also export the runServer command so other code (for instance, test code) can start the server as needed.
if (require.main === module) {
runServer().catch(err => console.error(err));
}
module.exports = { app, runServer, closeServer };