-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
73 lines (63 loc) · 1.68 KB
/
Copy pathexample.js
File metadata and controls
73 lines (63 loc) · 1.68 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
72
73
"use strict";
const path = require("node:path");
const { createWaypoint } = require("./src/waypoint");
const app = createWaypoint({ name: "Waypoint HW1" });
app.static("/static", path.join(__dirname, "public"));
app.get("/")
.as("home page")
.handle((req, res) => {
res.html(`<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/static/style.css">
<title>Waypoint HW1</title>
</head>
<body>
<main>
<p class="eyebrow">HTTP over raw TCP</p>
<h1>Waypoint is running.</h1>
<p>This page came from a route handler. The stylesheet came from the static file server.</p>
<nav>
<a href="/api/hello?name=visitor">JSON hello</a>
<a href="/api/users/42">Route params</a>
<a href="/__routes">Route explorer</a>
</nav>
</main>
</body>
</html>`);
});
app.get("/api/hello")
.as("query-string greeting")
.handle((req, res) => {
res.json({
message: `Hello ${req.query.name || "there"}`,
query: req.query
});
});
app.get("/api/users/:id")
.as("route parameter demo")
.handle((req, res) => {
res.json({
id: req.params.id,
name: `User ${req.params.id}`,
source: "route params"
});
});
app.post("/api/echo")
.as("manual JSON body parser demo")
.handle((req, res) => {
res.status(201).json({
received: req.body,
rawBytes: req.rawBody.length
});
});
app.explorer("/__routes");
if (require.main === module) {
const port = Number(process.env.PORT || 3000);
app.listen(port, () => {
console.log(`Waypoint HW1 running at http://localhost:${port}`);
});
}
module.exports = app;