-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
172 lines (149 loc) · 3.74 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const express = require('express');
const inertia = require('inertia-node');
const bodyParser = require('body-parser');
const crypto = require('crypto');
const fs = require('fs');
const app = express();
const port = 4000;
const ASSET_VERSION = 1;
const environment = process.env.ENV || 'development';
console.log(`Running in ${environment}`);
// In development, fetch the index built by vite on localhost:3000.
// In production, read it from dist.
const html = (pageString, viewData) => `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<!-- Custom data -->
<title>${viewData.title}</title>
<!-- Assets -->
${viteClientTag()}
${mainScriptTag()}
</head>
<!-- The Inertia page object -->
<body id="app" data-page='${pageString}'></body>
</html>
`;
function viteClientTag() {
if (environment === 'production') return '';
return `<script defer type="module" src="http://localhost:3000/@vite/client"></script>`;
}
function mainScriptTag() {
if (environment === 'production') {
// Read the manifest file and get it's import path
const raw = fs.readFileSync('dist/manifest.json', 'utf8')
const manifest = JSON.parse(raw);
return `<script defer type="module" src="${manifest['src/main.js']['file']}"></script>`
} else {
return `<script defer type="module" src="http://localhost:3000/src/main.js"></script>`;
}
}
// Yes I want to see request bodies }:|
app.use(bodyParser.json());
// Setup inertia middleware
app.use(inertia(html, ASSET_VERSION));
// Serve static assets
if (environment === 'production') {
app.use(express.static('dist'));
} else {
app.use(express.static('public'));
}
// Setup catalog and cart
global.bikes = [
{
id: 1,
brand: 'Specialized',
name: 'Allez',
ebike: false,
image: 'allez.png',
price_display: '$1,000',
price_int: 1000
},
{
id: 2,
brand: 'Trek',
name: 'Checkpoint ALR4',
ebike: false,
image: 'checkpoint.webp',
price_display: '$1,699',
price_int: 1699
},
{
id: 3,
brand: 'Van Moof',
name: 'S3',
ebike: true,
image: 's3.webp',
price_display: '$2,298',
price_int: 2490
},
{
id: 4,
brand: 'Cowboy',
name: '4',
ebike: true,
image: '4.png',
price_display: '$2,490',
price_int: 2490
},
]
global.cart = [
]
app.use(({ Inertia }, _, next) => {
Inertia.shareProps({ username: "Mark Cavendish" })
next();
});
// GET /home
app.get('/', (req, res) => {
req.Inertia.setViewData({ title: "Home Page" }).render({
component: "root/home",
props: {},
});
});
// GET /bikes
app.get('/bikes', (req, res) => {
req.Inertia.setViewData({ title: "Bikes Page" }).render({
component: "root/bikes",
props: {
bikes: bikes,
cart: cart,
},
});
});
// POST /bikes (add to cart)
app.post('/bikes', (req, res) => {
let bike = bikes.find(b => b.id == req.body.id);
if (bike) {
bike = Object.assign(bike, {});
bike.cart_id = crypto.randomBytes(10).toString('hex');
cart.push(bike);
}
req.Inertia.setViewData({ title: "Bikes Page" }).render({
component: "root/bikes",
props: {
bikes: bikes,
cart: cart,
confirm_message: bike ? 'Added to cart' : "That didn't work."
},
});
});
// GET /cart
app.get('/cart', (req, res) => {
console.log(cart);
req.Inertia.setViewData({ title: "Cart" }).render({
component: "root/cart",
props: {
cart: cart,
},
});
});
// DELETE /cart (remove from cart)
app.delete('/cart', (req, res) => {
cart = cart.filter(i => i.cart_id != req.body.cart_id);
req.Inertia.redirect('/cart');
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})