forked from rossmc/how-to-venia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
82 lines (77 loc) · 2.38 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
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'test';
}
const validEnv = require('./validate-environment')(process.env);
const {
Utilities: { addImgOptMiddleware }
} = require('@magento/pwa-buildpack');
const {
bestPractices,
createUpwardServer,
envToConfig
} = require('@magento/upward-js');
async function serve() {
const config = Object.assign(
{
bindLocal: true,
logUrl: true
},
envToConfig(validEnv),
{
env: validEnv,
before: app => {
addImgOptMiddleware(app, validEnv);
app.use(bestPractices());
}
}
);
if (validEnv.isProduction) {
if (process.env.PORT) {
console.log(
`NODE_ENV=production and PORT set. Binding to localhost:${
process.env.PORT
}`
);
config.port = process.env.PORT;
} else {
console.log(
`NODE_ENV=production and no PORT set. Binding to localhost with random port`
);
config.port = 0;
}
await createUpwardServer(config);
console.log(`UPWARD Server listening in production mode.`);
return;
}
if (!config.host) {
try {
const {
Utilities: { configureHost }
} = require('@magento/pwa-buildpack');
const { hostname, ports, ssl } = await configureHost({
interactive: false,
subdomain: validEnv.MAGENTO_BUILDPACK_SECURE_HOST_SUBDOMAIN,
exactDomain:
validEnv.MAGENTO_BUILDPACK_SECURE_HOST_EXACT_DOMAIN,
addUniqueHash:
validEnv.MAGENTO_BUILDPACK_SECURE_HOST_ADD_UNIQUE_HASH
});
config.host = hostname;
config.https = ssl;
config.port = ports.staging;
} catch (e) {
console.log(
'Could not configure or access custom host. Using loopback...'
);
}
}
await createUpwardServer(config);
if (config.logUrl) {
console.log('\nStaging server running at the address above.\n');
} else {
console.log('\nUPWARD server listening in staging mode.\n');
}
}
console.log('Launching staging server...\n');
serve();