-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
69 lines (63 loc) · 2.01 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
const express = require('express');
const uncaughtExceptions = require('./src/exceptions/uncaughtExceptions');
const bodyParser = require('body-parser');
const session = require('express-session');
const cookie = require('cookie-parser');
const mongoose = require('mongoose');
const morgan = require('morgan');
const apiRouter = require('./src/routes/apiRoutes');
const http = require('http');
const config = require('./src/config/config');
const path = require('path');
const cors = require('cors');
const helmet = require('helmet');
const connectToMongodb = require('./src/library/mongoDbConnection');
const compression = require('compression');
require('dotenv').config();
const port = config.app.serverPort || 4000;
const mongoConfig = {
devDbURI: config.db.testURI,
dbOptions: config.db.dbOptions
}
const corsOptions = {
origin: 'http://localhost:3000',
optionsSuccessStatus: 200
}
const app = express();
app.disable('x-powered-by');
app.use(helmet( { contentSecurityPolicy: false } ));
connectToMongodb(mongoose, mongoConfig);
app.set('views', path.join(__dirname, 'src', 'views'));
app.set('view engine', 'ejs');
app.use(uncaughtExceptions);
app.use(morgan('dev'));
app.use(cors(corsOptions));
app.use(compression());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(cookie( config.secret.cookieSecret));
app.use(session({
secret: config.secret.sessionSecret,
resave:true,
saveUninitialized:true
}));
app.use('/api/v1/', apiRouter);
app.use(( req, res)=> {
res.status(404).json({Error: true, message: 'API endpoint does not exist'});
})
app.use((err, req, res, next)=> {
console.error(err);
next(err);
});
app.use((err, req, res, next)=> {
res.status(500).json({ServerError: true, message: 'internal server error'});
});
function startServer() {
var server = http.createServer(app).listen(port, ()=> console.log(`app started at port ${port}`));
return server;
}
if(require.main === module) {
startServer();
}else{
module.exports = startServer;
}