This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
194 lines (150 loc) · 5.12 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const express = require("express");
const layouts = require("express-ejs-layouts")
const cookieParser = require("cookie-parser")
const bodyParser = require("body-parser")
const multer = require('multer')
const upload = multer()
const app = express();
// Ensure we use environment port if available for deploying
const PORT = process.env.PORT || 45678
app.set("view engine", "ejs")
app.use(express.static("public"))
app.use(express.static("json"))
app.use('/dist', express.static(__dirname + '/node_modules/@hotwired/turbo/dist/'));
app.use(cookieParser())
app.use(layouts)
// Turbo version
app.use((request, response, next) => {
response.locals.useTurbolinks = request.cookies.turbolinks || request.query.turbolinks
if (response.locals.useTurbolinks) {
// Set cookie for this session
response.cookie("turbolinks", "1")
} else {
response.clearCookie("turbolinks")
}
next()
})
// Determine platform
app.use((request, response, next) => {
const userAgent = request.get("User-Agent")
response.locals.native_app = userAgent.includes("Turbo Native")
next()
})
// Auth
app.use((request, response, next) => {
response.locals.authenticated = request.cookies && request.cookies.authenticated
next()
})
// Logging
app.use((request, response, next) => {
console.log(`${Date()} -- ${request.method} ${request.path}`)
next()
})
// JSON
app.use((request, response, next) => {
if (request.url.endsWith(".json")) {
res.type('json')
}
next()
})
// Routes
app.get("/", (request, response) => {
response.render("index", { title: "Turbo Native Demo", page_class: "index" })
})
app.get("/demo-one", (request, response) => {
response.render("one", { title: "How’d You Get Here?" })
})
app.get("/demo-two", (request, response) => {
response.render("two", { title: "Push or Replace?", action: request.query.action })
})
app.get("/demo-long", (request, response) => {
response.render("long", { title: "A Really Long Page" })
})
app.get("/demo-scroll", (request, response) => {
response.render("scroll", { title: "Restoring Your Scroll" })
})
app.get("/demo-follow", (request, response) => {
response.redirect("/redirected")
})
app.get("/demo-redirected", (request, response) => {
response.render("redirected", { title: "Redirected Page" })
})
app.get("/demo-reference", (request, response) => {
response.render("reference", { title: "Reference", page_class: "index" })
})
app.get("/demo-files", (request, response) => {
response.render("files", { title: "Handling Files" })
})
app.get("/demo-new", (request, response) => {
response.render("new", { title: "A Modal Webpage" })
})
app.post("/demo-new", (request, response) => {
response.redirect("/demo-success")
})
app.get("/demo-tasks", (request, response) => {
response.render("tasks", { title: "Tasks" })
})
app.get("/demo-tasks/wizard/step-1", (request, response) => {
response.render("wizard_1", { title: "A Modal Webpage (step 1)" })
})
app.post("/demo-tasks/wizard/step-1", (request, response) => {
response.redirect("/demo-tasks/wizard/step-2")
})
app.get("/demo-tasks/wizard/step-2", (request, response) => {
response.render("wizard_2", { title: "A Modal Webpage (step 2)" })
})
app.post("/demo-tasks/wizard/step-2", (request, response) => {
response.redirect("/demo-tasks?after_wizard_state=1")
})
app.get("/demo-success", (request, response) => {
response.render("success", { title: "It Worked!" })
})
app.get("/demo-numbers", (request, response) => {
response.render("numbers", { title: "A List of Numbers" })
})
app.get("/demo-nonexistent", (request, response) => {
response.status(404).send("Not Found")
})
app.get("/demo-reference/turbo-drive", (request, response) => {
response.render("turbo-drive", { title: "Turbo Drive" })
})
app.get("/demo-reference/turbo-frames", (request, response) => {
response.render("turbo-frames", { title: "Turbo Frames" })
})
app.get("/demo-reference/turbo-streams", (request, response) => {
response.render("turbo-streams", { title: "Turbo Streams" })
})
app.get("/demo-reference/turbo-native", (request, response) => {
response.render("turbo-native", { title: "Turbo Native" })
})
app.get("/demo-protected", (request, response) => {
if (response.locals.authenticated) {
response.render("protected", { title: "Protected Webpage" })
} else {
response.status(401).send("Unauthorized")
}
})
app.get("/demo-signin", (request, response) => {
response.render("signin", { title: "Sign In" })
})
app.post("/demo-signin", upload.none(), (request, response) => {
// Cookie expires in one day
const expiration = new Date(Date.now() + 86400000)
response.cookie("authenticated", request.body.name, { expires: expiration, httpOnly: true })
response.redirect("/")
})
app.post("/demo-signout", (request, response) => {
response.clearCookie("authenticated")
response.redirect("/")
})
app.get("/demo-slow", (request, response) => {
setTimeout(() => {
response.render("slow", { title: "Slow-loading Page" })
}, 3000)
})
app.get("/demo-test", (request, response) => {
response.sendStatus(200)
})
const listener = app.listen(PORT, () => {
console.log("Server is listening on port " + listener.address().port);
})