This repository has been archived by the owner on Oct 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
114 lines (94 loc) · 2.79 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict';
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const logger = require('morgan');
const bodyParser = require('body-parser');
//const LEX = require('letsencrypt-express');
const cookieParser = require('cookie-parser');
const expressValidator = require('express-validator');
const express = require('express');
const app = express();
/**
* Middleware setup (gross)
*/
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator([]));
app.use(cookieParser());
/**
* API setup
*/
const api = require('./lib/api');
app.use('/', api);
app.get('/404', (request, response) => {
response.status(404).json({"error": "page not found"});
});
/**
* Letsencrypt and other stuff below
*/
/*const lex = LEX.create({
server: 'staging',
//configDir: require('os').homedir() + '/letsencrypt/etc',
configDir: '/etc/letsencrypt',
approveDomains: approveDomains
});
function approveDomains(opts, certs, cb) {
// This is where you check your database and associated
// email addresses with domains and agreements and such
// The domains being approved for the first time are listed in opts.domains
// Certs being renewed are listed in certs.altnames
if (certs) {
opts.domains = certs.altnames;
} else {
opts.email = 'domfarolino@gmail.com';
opts.agreeTos = true;
}
// NOTE: you can also change other options such as `challengeType` and `challenge`
// opts.challengeType = 'http-01';
// opts.challenge = require('le-challenge-fs').create({});
cb(null, { options: opts, certs: certs });
}
lex.onRequest = app;
*/
require('http').createServer(app).listen(process.env.PORT, function() {
console.log('Server created!');
});
/*
require('http').createServer(lex.middleware(app)).listen(process.env.PORT, function () {
console.log("Listening for ACME http-01 challenges on", this.address());
});
*/
/*
require('https').createServer(lex.httpsOptions, lex.middleware(app)).listen(process.env.HTTPS_CHALLENGE_PORT, function () {
console.log("Listening for ACME tls-sni-01 challenges and serve app on", this.address());
});
*/
// catch 404 and forward to error handler
app.use(function (req, res, next) {
res.redirect('/404');
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, request, response, next) {
response.status(err.status || 500);
response.json({
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, request, response, next) {
response.status(err.status || 500);
response.json({
message: err.message,
error: {}
});
});
module.exports = app;