-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
81 lines (65 loc) · 2.09 KB
/
app.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
76
77
78
79
80
81
'use strict';
const debug = require('debug')('calendar:app');
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const compress = require('compression');
const methodOverride = require('method-override');
const helmet = require('helmet');
const cors = require('cors');
const yearRouter = require('./routes/year');
module.exports = () => {
// Initialize express app
debug('Initializing a new express app...');
const app = express();
// set the environment to be development
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'development';
}
// Set up CORS
app.use(cors());
// Passing the request url to environment locals
app.use((req, res, next) => {
res.locals.url = req.protocol + '://' + req.headers.host + req.url;
next();
});
// Should be placed before express.static
app.use(compress({
filter : (req, res) => {
return (/json|text|javascript|css/).test(res.getHeader('Content-Type'));
},
level : 9
}));
// Showing stack errors
app.set('showStackError', true);
// Environment dependent middleware
if (process.env.NODE_ENV === 'development') {
// Enable logger (morgan)
app.use(morgan('dev'));
// Disable views cache
app.set('view cache', false);
} else if (process.env.NODE_ENV === 'production') {
app.locals.cache = 'memory';
}
// Request body parsing middleware should be above methodOverride
app.use(bodyParser.urlencoded({
extended : true
}));
app.use(bodyParser.json());
app.use(methodOverride());
// Use helmet to secure Express headers
app.use(helmet.frameguard());
app.use(helmet.xssFilter());
app.use(helmet.noSniff());
app.use(helmet.ieNoOpen());
app.disable('x-powered-by');
const router = express.Router(); // get an instance of the express Router
router.get('/', (req, res) => {
res.json({message : 'Welcome to Kollavarsham API!!!'});
});
router.use('/', yearRouter);
// all of our routes will be prefixed with /api
app.use('/api', router);
// Return Express server instance
return app;
};