-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.js
177 lines (137 loc) · 4.43 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
import express from "express"
import layouts from "express-ejs-layouts"
import cookieParser from "cookie-parser"
import multer from "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("public/javascript"))
app.use(express.static("json"))
app.use(cookieParser())
app.use(layouts)
// 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("/one", (request, response) => {
response.render("one", { title: "How’d You Get Here?" })
})
app.get("/two", (request, response) => {
response.render("two", { title: "Push or Replace?", action: request.query.action })
})
app.get("/long", (request, response) => {
response.render("long", { title: "A Really Long Page" })
})
app.get("/scroll", (request, response) => {
response.render("scroll", { title: "Restoring Your Scroll" })
})
app.get("/follow", (request, response) => {
response.redirect("/redirected")
})
app.get("/redirected", (request, response) => {
response.render("redirected", { title: "Redirected Page" })
})
app.get("/reference", (request, response) => {
response.render("reference", { title: "Reference", page_class: "index" })
})
app.get("/files", (request, response) => {
response.render("files", { title: "Handling Files" })
})
app.get("/new", (request, response) => {
response.render("new", { title: "A Modal Webpage" })
})
app.post("/new", (request, response) => {
response.redirect("/success")
})
app.get("/strada-form", (request, response) => {
response.render("strada-form", { title: "Strada" })
})
app.post("/strada-form", (request, response) => {
setTimeout(() => {
response.redirect("/success")
}, 1500)
})
app.get("/strada-menu", (request, response) => {
response.render("strada-menu", { title: "Strada" })
})
app.get("/strada-overflow", (request, response) => {
response.render("strada-overflow", { title: "Strada" })
})
app.get("/success", (request, response) => {
response.render("success", { title: "It Worked!" })
})
app.get("/numbers", (request, response) => {
response.render("numbers", { title: "A List of Numbers" })
})
app.get("/nonexistent", (request, response) => {
response.status(404).send("Not Found")
})
app.get("/reference/turbo-drive", (request, response) => {
response.render("turbo-drive", { title: "Turbo Drive" })
})
app.get("/reference/turbo-frames", (request, response) => {
response.render("turbo-frames", { title: "Turbo Frames" })
})
app.get("/reference/turbo-streams", (request, response) => {
response.render("turbo-streams", { title: "Turbo Streams" })
})
app.get("/reference/turbo-native", (request, response) => {
response.render("turbo-native", { title: "Turbo Native" })
})
app.get("/protected", (request, response) => {
if (response.locals.authenticated) {
response.render("protected", { title: "Protected Webpage" })
} else {
response.status(401).send("Unauthorized")
}
})
app.get("/signin", (request, response) => {
response.render("signin", { title: "Sign In" })
})
app.post("/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("/signout", (request, response) => {
response.clearCookie("authenticated")
response.redirect("/")
})
app.get("/slow", (request, response) => {
setTimeout(() => {
response.render("slow", { title: "Slow-loading Page" })
}, 3000)
})
app.get("/test", (request, response) => {
response.sendStatus(200)
})
const listener = app.listen(PORT, () => {
console.log("Server is listening on port " + listener.address().port);
})