-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
90 lines (70 loc) · 2.34 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
82
83
84
85
86
87
88
89
90
let express = require('express');
//let apicache = require('apicache');
let compression = require('compression');
let path = require('path');
let favicon = require('serve-favicon');
let cookieParser = require('cookie-parser');
let logger = require('morgan');
let bodyParser = require('body-parser');
let cors = require('cors');
const YAML = require('yamljs');
let fs = require('fs');
let swaggerUi = require('swagger-ui-express'),
swaggerDocument = YAML.load('./throughput.yaml');
let app = express();
//let cache = apicache.middleware;
app.use(cors({credentials: true, origin: true}))
// app.use(cache('15 minutes'));
// create a write stream (in append mode)
let accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), {
flags: 'a'
})
// setup the logger
app.use(logger(':date[iso]\t:remote-addr\t:method\t:url\t:status\t:res[content-length]\t:response-time[0]\t:user-agent', {
stream: accessLogStream
}))
// 3. (optionally) Serve the OpenAPI spec
const spec = path.join(__dirname, 'throughput.yaml');
let routes = require('./routes/index');
let auth = require('./routes/auth');
// let debug = require('debug')('app4')
let options = {
swaggerUrl: 'https://throughputdb.com/api-docs',
customCssUrl: '/custom.css'
}
app.use(compression());
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument, options));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
app.all('*', function(req, res, next) {
res.redirect('/api-docs');
});
app.listen(3000)