-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
75 lines (60 loc) · 1.85 KB
/
index.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
72
73
74
75
import express from 'express';
import logger from 'morgan';
import helmet from 'helmet';
import { json, urlencoded } from 'body-parser';
import cors from 'cors';
import dbConnection from './src/utils/dbConnection';
// import environmental variables from our variables.env file
require('dotenv').config({ path: '.env' });
//import module
import routes from './src/route';
import {
notFound,
developmentErrors,
productionErrors
} from './src/utils/errorHandlers';
// Connect to our Database and handle an bad connections
dbConnection();
// init Express app
const app = express();
// having fun
app.use((req, res, next) => {
res.header('X-powered-by', 'Blood, sweat, and tears.');
next();
});
//route logger
app.use(logger('dev'));
//use helmet security wrapper
app.use(helmet());
// Takes the raw requests and turns them into usable properties on req.body
app.use(json());
app.use(urlencoded({ extended: true }));
//handling CORS
app.use(cors());
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, Authorization'
);
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'POST, PUT, GET, DELETE, PATCH');
return res.status(200).json({});
}
next();
});
//use route module
app.use('/', routes);
// If that above routes didnt work, we 404 them and forward to error handler
app.use(notFound);
// Otherwise this was a really bad error we didn't expect! Shoot eh
if (app.get('env') === 'development') {
/* Development Error Handler - Prints stack trace */
app.use(developmentErrors);
}
// production error handler
app.use(productionErrors);
app.set('port', process.env.PORT);
const server = app.listen(app.get('port'), () => {
console.log(`App running 🏃 on PORT ${server.address().port}`);
});