-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
executable file
·68 lines (54 loc) · 1.8 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
'use strict';
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const express = require('express');
const path = require('path');
const nconf = require('nconf');
const morgan = require('morgan');
const consign = require('consign');
const config = require('./config.js');
const compression = require('compression');
const cors = require('cors');
const app = express();
// load settings from environment config
const { NODE_ENV } = process.env;
nconf.argv(config);
nconf.argv().env().file({
'file': `./config/auth.${NODE_ENV || 'development'}.json`
});
module.exports.nconf = nconf;
app.nconf = nconf;
// log all requests
app.use(morgan('combined'));
// support json and url encoded requests
app.use(bodyParser.urlencoded(config.bodyParser));
app.use(bodyParser.json(config.bodyParser));
app.use(bodyParser.raw(config.bodyParser));
app.use(compression());
// setup encrypted session cookies
app.use(cookieParser());
app.use(cors());
// statically serve from the 'public' folder
app.use(express.static(path.join(__dirname, 'public')));
// use html to render the views
app.set('views', path.join(__dirname, 'public'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
// use the environment's port if specified
app.set('port', process.env.PORT || 8080);
// configure routes
consign(config.consign)
.include('routes')
.into(app);
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
});
const SERVER_PORT = app.get('port');
app.listen(SERVER_PORT, function() {
console.log('Your server is listening at http://localhost:%d', SERVER_PORT);
});
module.exports = app;