forked from amazon-archives/startup-kit-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
56 lines (42 loc) · 1.31 KB
/
app.js
File metadata and controls
56 lines (42 loc) · 1.31 KB
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
"use strict";
const log = require('./util/log');
// nconf configures the variable overriding hierarchy:
// command line -> environment variables -> config file
const nconf = require('nconf');
nconf.argv()
.env()
.file({ file:'config.json'});
nconf.defaults({
AUTH_ENABLED: false,
AUTH_JWT_SECRET: 'my-secret',
AUTH_JWT_TOKENTIME: 6000,
});
// set up Express and routes
const express = require('express'),
bodyParser = require('body-parser'),
responseTime = require('response-time');
const app = express();
app.use(bodyParser.json());
// response time measurement
app.use(responseTime( (req, res, time) => {
let stat = (req.method + req.url)
.toUpperCase()
.replace(/[:\.]/g, '')
.replace(/\//g, '_');
log.info(`${stat}_latency ${time}`);
}));
// CORS enablement
app.all('/*', (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization,Accept');
if ('OPTIONS' == req.method) {
res.status(200).end();
} else {
next();
}
});
require('./routes/api')(app, express);
// BEGIN LISTENING
app.listen(8081);
log.info('Listening on port 8081 . . . .');