-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (42 loc) · 1.34 KB
/
server.js
File metadata and controls
51 lines (42 loc) · 1.34 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
require('dotenv').config();
const express = require('express');
const path = require('path');
const fs = require('fs');
const http = require('http');
const https = require('https');
const morgan = require('morgan');
const app = express();
app.use(morgan('tiny'));
const countHits = require('./utils/hitCounter')
app.get('/', (req, res) => {
const indexPath = path.join(__dirname, 'public/index.html');
res.sendFile(indexPath);
});
app.get('/logo', (req, res) => {
const indexPath = path.join(__dirname, 'public/logo.png');
res.sendFile(indexPath);
});
app.use(countHits)
app.use((req, res, next) => {
if (req.path !== '/logo.png' && req.path !== '/') {
return res.status(404).send('404 Not Found');
}
next();
});
app.use(express.static(__dirname, { dotfiles: 'allow' }));
const privateKey = fs.readFileSync(process.env.privkey, process.env.encoding);
const certificate = fs.readFileSync(process.env.cert, process.env.encoding);
const ca = fs.readFileSync(process.env.chain, process.env.encoding);
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(80, () => {
console.log('HTTP Server running on port 80');
});
httpsServer.listen(443, () => {
console.log('HTTPS Server running on port 443');
});