-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
102 lines (92 loc) · 3.2 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
// Import necessary modules
const express = require('express');
const app = express();
const path = require('path');
const cookieParser = require('cookie-parser');
const cors = require('cors');
require('dotenv').config(); // Load environment variables from .env file
// Configure CORS (Cross-Origin Resource Sharing) for handling cross-origin requests
app.use(cors({
origin: true,
credentials: true
}));
// Middleware to parse JSON and URL-encoded request bodies
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Middleware to parse cookies from the request headers
app.use(cookieParser());
// Middleware to serve static files from the "public" directory
app.use(express.static(path.join(__dirname, 'public')));
// Configure EJS (Embedded JavaScript templates) as the view engine for rendering
app.set('view engine', 'ejs');
app.set('views', __dirname); // Set the views directory
// Middleware to set global variables for use in EJS templates
app.use((req, res, next) => {
res.locals.commonPath = path.join(__dirname, 'src/common');
res.locals.protocol = process.env.protocol;
res.locals.domainA = process.env['domain-a'];
res.locals.domainB = process.env['domain-b'];
res.locals.domainC = process.env['domain-c'];
res.locals.googleClientId = process.env['google-client-id'];
res.locals.facebookAppId = process.env['facebook-app-id'];
res.locals.recaptchaSiteKey = process.env['recaptcha-site-key'];
res.locals.port = process.env.port;
res.locals.isPortPresent = req.get('host').includes(':');
res.locals.currentDomain = req.get( 'host' );
switch ( res.locals.currentDomain ) {
case res.locals.domainA:
res.locals.backgroundColor = process.env['domain-a-background'];
break;
case res.locals.domainB:
res.locals.backgroundColor = process.env['domain-b-background'];
break;
case res.locals.domainC:
res.locals.backgroundColor = process.env['domain-c-background'];
break;
default:
res.locals.backgroundColor = 'bg-gray-100';
}
next(); // Proceed to the next middleware or route handler
});
// Mount routes for different demo types
const demoTypes = [
'chips',
'related-websites-sets',
'private-state-tokens',
'fedcm',
'storage-access-api',
'frame-overlay',
];
demoTypes.forEach(demoType => {
const demoRoutes = require(`./src/demos/${demoType}/routes`);
app.use(`/${demoType}`, demoRoutes); // Mount the routes on a path specific to the demo type
});
// Mount routes for different scenarios
const scenarios = [
'ecommerce',
'single-sign-on',
'analytics',
'embedded-video',
'payment-gateway',
'personalization',
'personalization-localstorage',
'gsi',
'facebook-like',
'facebook-comments',
'disqus-comments',
'spotify'
];
scenarios.forEach(scenario => {
const scenarioRoutes = require(`./src/scenarios/${scenario}/routes`);
app.use(`/${scenario}`, scenarioRoutes); // Mount the routes on a path specific to the scenario
});
// Catch-all route handler for unmatched routes, rendering the default page
app.use((req, res) => {
res.render(path.join(__dirname, 'src/common/index'), {
title: 'PSAT Demos'
});
});
// Start the server and listen on the specified port
app.listen(process.env.port, () => {
console.log('Server is running on port ' + process.env.port);
});