This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
101 lines (81 loc) · 2.73 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
require("es6-promise").polyfill();
require("fetch-everywhere");
import dotenv from "dotenv"; //loading .env variables
dotenv.load();
import compression from "compression";
import express from "express";
import expstate from "express-state";
import logger from "morgan";
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import path from "path";
import React from "react";
import { renderToString } from "react-dom/server";
import MovieApp from "./src/client/components/App";
const app = express();
const apiToken = [process.env.API_TOKEN];
const server_url = process.env.PRODUCTION_URL || "http://localhost:8000";
expstate.extend(app);
app.set("state namespace", "MovieRoulette");
app.use(compression());
app.set("view engine", "pug");
app.set("views", path.join(__dirname, "./src/views"));
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use("/", express.static(__dirname + "/build/"));
// Require our routes into the application.
require("./src/server/routes")(app);
function _loadMovie(req, res) {
return fetch(`${server_url}/proxy/movie/`)
.then(response => response.json())
.then(json => {
return json;
});
}
function _loadList(req, res) {
return fetch(`${server_url}/proxy/lists/${req.params.listkey}`)
.then(response => response.json())
.then(json => {
return json;
});
}
app.get("/", (req, res) => {
const data = {};
const movie = _loadMovie(req, res);
movie.then((movie) => {
data.movie = movie;
data.list = false;
const initialState = data;
const jsx = <MovieApp {...initialState} />;
const reactDom = renderToString(jsx);
res.expose(initialState, "MovieRoulette.initialState");
res.render("index", {
react: reactDom,
initialState: { initialState },
production: process.env.PRODUCTION === "true" ? true : false
});
});
});
app.get("/list/:listkey", (req, res) => {
let data = {};
const movie = _loadMovie(req, res);
movie.then((movie) => {
data.movie = movie;
const list = _loadList(req, res);
list.then((list) => {
data.list = list;
const initialState = data;
const jsx = <MovieApp {...initialState} />;
const reactDom = renderToString(jsx);
res.expose(initialState, "MovieRoulette.initialState");
res.render("index", {
react: reactDom,
initialState: { initialState },
production: process.env.PRODUCTION === "true" ? true : false
});
})
});
});
module.exports = app;