-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (64 loc) · 2.54 KB
/
index.js
File metadata and controls
77 lines (64 loc) · 2.54 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
const express = require('express');
const compression = require('compression')
const fs = require ('fs');
const HTMLConverter = require ('./scripts/server/html-converter');
const WebTokenMiddleware = require ('./scripts/server/web-token.middleware');
const PathMelioratorMiddleware = require ('./scripts/server/path-meliorator.middleware');
const Configuration = require ('./config.json');
//Create Express app
const app = express();
//Minify in Production
if (Configuration.application.production) {
app.use(compression ());
//TODO: Add Minifying
}
//Make needed Modules and Files Static
app.use('/scripts/base-element.js', express.static('scripts/base-element.js'));
app.use('/node_modules/lit-html', express.static('node_modules/lit-html'));
app.use ('/' + Configuration.application.componentsDirectory, express.static (Configuration.application.componentsDirectory));
//Make the Asset-Folders static
Configuration.application.assetDirectories.forEach ((assetFolder) => {
app.use ('/' + assetFolder, express.static (assetFolder));
});
//Render Static Pages, defined in the `pagesDirectory`
app.get('/*', PathMelioratorMiddleware.MelioratePath, (req, res) => {
//Read the filePath
let filePath = req.filePath;
let htmlConverter = new HTMLConverter (
filePath
);
htmlConverter.exportHTML ()
.then (renderedHTML => {
res.type('text/html');
return res.status (200).send (renderedHTML);
})
.catch (error => {
console.error ("Error:", error);
let notFoundContent = fs.readFileSync (Configuration.application.pagesDirectory + '/' + Configuration.application.errorPage, 'utf8');
return res.status (404).send (notFoundContent);
});
});
//Render Dynamic Pages with API
app.post('/*', WebTokenMiddleware.WebToken, async (req, res) => {
//Read the Raw HTML-File from Request
let template = '';
req.on('data', chunk => {
template += chunk.toString(); // convert Buffer to string
});
await req.on('end', () => {});
let htmlConverter = new HTMLConverter (
template,
null
);
htmlConverter.exportHTML ()
.then (renderedHTML => {
res.type('text/html');
return res.status (200).send (renderedHTML);
})
.catch (error => {
console.error ("Error:", error);
return res.status (400).send ("Error rendering received template!");
});
});
app.listen(process.env.PORT || 3000);
console.log('Server listening on Port: ' + (process.env.PORT || 3000));