-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (62 loc) · 1.46 KB
/
Copy pathindex.js
File metadata and controls
71 lines (62 loc) · 1.46 KB
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
import React from "react";
import express from "express";
import { renderToString } from "react-dom/server";
import { readFileSync } from "fs";
import { App } from "../client/App";
import { handleModifyAnswerVotes } from "../shared/utility";
const data = {
questions: [
{
questionId: "Q1",
content: "Should we use axios or Fetch for AJAX?",
},
{
questionId: "Q2",
content: "Should we use axios or Fetch for AJAX?",
},
],
answers: [
{
answerId: "A1",
questionId: "Q1",
upvotes: 2,
content: "axios",
},
{
answerId: "A2",
questionId: "Q1",
upvotes: 2,
content: "axios",
},
{
answerId: "A3",
questionId: "Q2",
upvotes: 2,
content: "axios",
},
],
};
const app = new express();
app.use(express.static("dist"));
app.get("/data", async (req, res) => {
res.json(data);
});
app.get("/vote/:answerId", (req, res) => {
const { query, params } = req;
data.answers = handleModifyAnswerVotes(
data.answers,
params.answerId,
+query.increment
);
res.send("Ok");
});
app.get("/", async (req, res) => {
const index = readFileSync(`public/index.html`, `utf8`);
console.log("answers array", data.answers);
const rendered = renderToString(<App {...data} />);
res.send(index.replace("{{rendered}}", rendered));
});
// console.lo("Hello from the outside.")
app.listen(3000, () => {
console.log("App is listening at PORT- 3000");
});