-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressServer_dev.js
More file actions
142 lines (113 loc) · 4.13 KB
/
ExpressServer_dev.js
File metadata and controls
142 lines (113 loc) · 4.13 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//ExpressServer_dev is the source, and gets babel-node built into ExpressServer for production
const bodyParser = require('body-parser');
const express = require('express');
const fs = require('fs');
const helmet = require('helmet');
const http = require('http');
// const https = require('https');
const path = require('path');
import webpack from 'webpack';
// import webpackMiddleware from 'webpack-dev-middleware'
// import webpackHot from 'webpack-hot-middleware'
import webpackConfig from './webpack.dev.config.js';
//app is for controlling the server
let app = express();
let httpServer = http.createServer(app);
// const privateKey = fs.readFileSync('C:/SSL/private.key');
// const certificate = fs.readFileSync('C:/SSL/certificate.crt');
// const credentials = { key: privateKey, cert: certificate };
// let httpsServer = https.createServer(credentials, app)
///////////////////////////////////////////////
// PLACE TO RUN SCRIPT ON SERVER START
///////////////////////////////////////////////
//////////////////////////////////////////////
//////////////////////////////////////////////
//////////////////////////////////////////////
//http requests with content type application/json will be parsed
//req.body will be changed to a json object
app
.use(bodyParser.json({ limit: '50mb' }))
.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))
// tells the browser to use https if they are on http
.use(helmet())
.set('view engine', 'hjs');
//router for gw2daata JSON responses
app.use('/gw2data', require('./server_routes/gw2data_route'));
if (process.env.NODE_ENV === 'development') {
const webpackDevMiddleware = require('webpack-dev-middleware')(
webpack(webpackConfig),
{
publicPath: webpackConfig.output.publicPath,
}
);
app.use(webpackDevMiddleware);
app.use(require('webpack-hot-middleware')(webpack(webpackConfig)));
app.get('/gw2data/*', (req, res, next) => {
const indexHTML = webpackDevMiddleware.fileSystem.readFileSync(
require('path').join(webpackConfig.output.path, 'index.html'),
'utf-8'
);
res.send(indexHTML);
});
}
//Easy routing for requesting html and images, etc.
app.use(express.static('website_public'));
//if request reaches this, then physical file doesn't exist
app
.get('/gw2data/*', (req, res, next) => {
res.sendFile(
path.resolve(__dirname, './website_public/gw2data/index.html')
);
})
//for that asshole that's just trying to POST over and over again
// app.post('/', (req, res, next) => {
// console.log (`that asshole that keeps POSTing did it again... IP: ${req.ip}`)
// res.status(403).send('fuck you')
// })
///////////////////////////////////
// ERROR RESPONSES
///////////////////////////////////
.use(function routeNotFound(req, res, next) {
let message =
`Not Found:\nRequest from: ${req.ip}\nhttp info:\n` +
`method: ${req.method}\nhost: ${req.hostname}\npath: ${req.path}\n`;
if (req.params) message += `params: ${JSON.stringify(req.params)}\n`;
if (req.query) message += `query: ${JSON.stringify(req.query)}\n`;
var err = new Error(message);
err.status = 404;
next(err);
})
.use(function errorHandler(err, req, res, next) {
console.error(err);
res.status(err.status || 500).send('Something went wrong!');
});
//
// function errorHandler(err, req, res, next){
// console.error (err);
// res.status(err.status || 500).send('Something went wrong!')
// }
if (process.env.NODE_ENV === 'development') {
//this starts the server
httpServer.listen(80, () => {
console.log('http listening on port 80');
});
}
//this starts the server with greenlock to renew SSL certs
else {
require('greenlock-express')
.init({
packageRoot: __dirname,
// contact for security and critical bug notices
maintainerEmail: 'zach.swalberg@gmail.com',
// where to look for configuration
configDir: './greenlock.d',
// whether or not to run at cloudscale
cluster: false,
})
// Serves on 80 and 443
// Get's SSL certificates magically!
.serve(app);
}
// httpsServer.listen(443, () => {
// console.log('https listening on port 443')
// })