forked from michaeljoepeterson/scholarlyAPIRequests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (35 loc) · 941 Bytes
/
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
require('dotenv').config();
const express = require('express');
const app = express();
const bodyParser = require("body-parser");
const jsonParser = bodyParser.json();
const {PORT} = require('./config');
function runServer( port = PORT) {
return new Promise((resolve, reject) => {
server = app.listen(port, () => {
console.log(`Your app is listening on port! ${port}`);
resolve();
})
.on('error', err => {
mongoose.disconnect();
reject(err);
});
});
}
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 (require.main === module) {
runServer().catch(err => console.error(err));
}
module.exports = { app, runServer, closeServer };