-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (40 loc) · 1.37 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
require('dotenv').config({ silent: true });
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const morgan = require('morgan');
const { errors } = require('celebrate');
const db = require('./web/models/db');
const jobs = require('./scripts/cron-jobs');
//allow cross origin requests
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
// setup logger
app.use(morgan('dev'));
// configure app to use bodyParser() this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const PORT = process.env.PORT || 3000;
// routes for api
const router = express.Router();
require('./web/routes/routes')(router);
// register routes... routes prefixed /api
app.use('/api', router);
// celebrate errors
app.use(errors());
// default catch-all route that sends back a error message in JSON format.
app.use('*', (req, res) => res.status(404).json({
message: 'Error - Not found'
}));
// start server
app.listen(PORT);
console.log('App running on port ' + PORT);
// start jobs
if (process.env.emailJob) {
console.log('Enabling Email Job');
jobs.emailJob.start();
}