-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (41 loc) · 1.26 KB
/
index.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
const express = require('express'),
app = express(),
template = require('./views/template')
path = require('path');
// Serving static files
app.use('/assets', express.static(path.resolve(__dirname, 'assets')));
app.use('/media', express.static(path.resolve(__dirname, 'media')));
// hide powered by express
app.disable('x-powered-by');
// start the server
app.listen(process.env.PORT || 3000);
// data model
const data = require('./assets/data.json');
let initialState = {
isFetching: false,
apps: data
}
// SSR function import
const ssr = require('./views/server');
// server rendered home page
app.get('/', (req, res) => {
const { preloadedState, content} = ssr(initialState)
const response = template("Server Rendered Page", preloadedState, content)
res.setHeader('Cache-Control', 'assets, max-age=604800')
res.send(response);
});
// Pure client side rendered page
app.get('/client', (req, res) => {
let response = template('Client Side Rendered page')
res.setHeader('Cache-Control', 'assets, max-age=604800')
res.send(response);
});
// stops server during local development
app.get('/exit', (req, res) => {
if(process.env.PORT) {
res.send("Sorry, the server denies your request")
} else {
res.send("shutting down")
process.exit(0)
}
});