-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
90 lines (77 loc) · 2.54 KB
/
server.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
const fs = require('fs');
const path = require('path');
const url = require('url');
const connect = require('connect');
const serveStatic = require('serve-static');
const bodyParser = require('body-parser');
const port = 3000;
//const http2 = require('http2');
//const certs = {
// key: fs.readFileSync(__dirname + '/.certs/localhost.key', 'utf8'),
// cert: fs.readFileSync(__dirname + '/.certs/localhost.crt', 'utf8'),
// allowHTTP1: true
//};
const app = connect('127.0.0.1');
app.use(serveStatic(path.join(__dirname, 'cartridge1')));
app.use(serveStatic(path.join(__dirname, 'cartridge2')));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use('/service-worker.js', function(req, res) {
res.writeHead(200, {'Content-Type': 'application/javascript'});
res.end(fs.readFileSync('./cartridge1/js/services/service-worker.js'));
});
app.use('/plp', function(req, res) {
const queryObject = url.parse(req.url, true).query;
let html;
for (let prop in queryObject) {
const url = `./pages/plp/${prop}/${queryObject[prop]}.html`;
if (fs.existsSync(url)) {
html = fs.readFileSync(url);
break;
}
}
if (!html) {
html = fs.readFileSync('./pages/plp.html');
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
});
app.use('/endpoint', function(req, res) {
const request = req.body;
let response = {success: 'success message'};
let head = {
code: 200,
contentType: {'Content-Type': 'application/json'}
}
switch (true) {
case (request.expectedResponse === 'formError'):
response.error = 'Some general error for form';
break;
case (request.expectedResponse === 'inputErrors'):
response.fields = {
'address': 'some error'
};
break;
case (request.expectedResponse === 'redirectUrl'):
response.redirectUrl = '/plp';
break;
case (request.expectedResponse === 'notJSON'):
head.contentType = {'Content-Type': 'text/html'};
response = 'someString';
break;
case (request.expectedResponse === 'status500'):
head.code = 500;
head.contentType = {'Content-Type': 'text/html'};
response = {
errorMessage: 'Some server side error message'
};
break;
}
res.writeHead(head.code, head.contentType);
const isTextType = request.expectedResponse === 'notJSON';
res.end(isTextType ? response : JSON.stringify(response));
});
app.listen(port);
// to check service worker 1) comment http2.createServer 2) change to app.listen(port);
//http2.createSecureServer(certs, app).listen(process.env.PORT || port);
console.log('Listen: http://127.0.0.1:' + (process.env.PORT || port));