-
Notifications
You must be signed in to change notification settings - Fork 51
/
server.js
executable file
·54 lines (40 loc) · 1.35 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
import express from 'express';
import helmet from 'helmet';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import cors from 'cors';
import morgan from 'morgan';
require ('dotenv').config();
const app = express();
// Add middleware for http headers
app.use(helmet())
/* Do not change the following line! It is required for testing and allowing
* the frontend application to interact as planned with the api server
*/
const PORT = process.env.PORT || 4001;
app.use(morgan('tiny'));
// Add middleware for handling CORS requests from index.html
app.use(cors());
// Add middleware for parsing request bodies here:
app.use(bodyParser.json());
// Mongoose options
const options = {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
};
mongoose
.connect(process.env.MONGODB_URL, options)
.then(() => console.log(`Database connection established`))
.catch((err) => console.error(`There was an error connecting to database, the err is ${err}`));
// Mount your existing apiRouter below at the '/api' path.
const apiRouter = require('./server/api');
app.use('/api', apiRouter);
// This conditional is here for testing purposes:
if (!module.parent) {
// Add your code to start the server listening at PORT below:
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
}
export default app;