You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -10,6 +10,7 @@ HTTP Functions are standard web services, not `exports.main(event, context)` han
10
10
- Listen on port `9000`.
11
11
- Ship an executable `scf_bootstrap` file.
12
12
- Include runtime dependencies in the package; HTTP Functions do not auto-install `node_modules` for you.
13
+
- For simple HTTP APIs, prefer the Node.js native `http` module so the function shape stays explicit and dependency-light. Only introduce Express, Koa, NestJS, or similar frameworks when the user explicitly asks for one or the service complexity justifies it.
if (req.method==="GET"&&url.pathname==="/health") {
78
+
sendJson(res, 200, { ok:true });
79
+
return;
80
+
}
42
81
43
-
app.use(express.json());
82
+
if (req.method==="POST"&&url.pathname==="/echo") {
83
+
try {
84
+
constbody=awaitreadJsonBody(req);
85
+
sendJson(res, 200, { received: body });
86
+
} catch (error) {
87
+
sendJson(res, 400, { error:error.message });
88
+
}
89
+
return;
90
+
}
44
91
45
-
app.get("/health", (req, res) => {
46
-
res.json({ ok:true });
92
+
sendJson(res, 404, { error:"Not Found" });
47
93
});
48
94
49
-
app.listen(9000);
95
+
server.listen(9000);
50
96
```
51
97
98
+
## Code-writing rules
99
+
100
+
- Do not write HTTP Functions as `exports.main = async (event, context) => {}`. That is the Event Function contract.
101
+
- Start an HTTP server explicitly with `http.createServer(...)` or a framework app, and always bind to port `9000`.
102
+
- Treat routing, method checks, and body parsing as part of the function code. With the native `http` module, parse `req.url` yourself and read the request body from the stream before calling `JSON.parse`.
103
+
- Return JSON responses explicitly and set `Content-Type` yourself, for example `application/json; charset=utf-8`.
104
+
- Keep unsupported routes and methods explicit. Return `404` for unknown paths, and return `405` when the path exists but the HTTP method is not allowed.
105
+
- Keep `scf_bootstrap`, `index.js`, `package.json`, and any bundled dependencies in the function directory that will be uploaded.
106
+
52
107
## Request handling rules
53
108
54
-
-`req.query` -> query string values.
55
-
-`req.body`-> parsed request body, but only after body-parsing middleware is configured.
109
+
-With Node native `http`, use `new URL(req.url, "http://127.0.0.1")` and read `url.searchParams` for query values.
110
+
-With Node native `http`, `req.body`does not exist. Read the body stream manually, then parse JSON yourself.
56
111
-`req.headers` -> incoming HTTP headers.
57
-
-`req.params` -> path parameters.
58
-
- Always send a response with `res.json()`, `res.send()`, or`res.status(...).json()`.
112
+
-Path parameters are framework-level conveniences. With the native `http` module, match `url.pathname` yourself.
113
+
- Always send a response explicitly. With Node native `http`, use `res.writeHead(...)` and`res.end(...)`.
59
114
- Return meaningful status codes such as `400`, `401`, `404`, `405`, `500`.
60
115
61
116
### Example with method checks
62
117
63
118
```javascript
64
-
constexpress=require("express");
65
-
constapp=express();
66
-
67
-
app.use(express.json());
68
-
69
-
app.post("/users", (req, res) => {
70
-
const { name, email } =req.body;
71
-
72
-
if (!name ||!email) {
73
-
returnres.status(400).json({ error:"name and email are required" });
if (url.pathname==="/users"&&req.method==="POST") {
156
+
try {
157
+
const { name, email } =awaitreadJsonBody(req);
158
+
159
+
if (!name ||!email) {
160
+
sendJson(res, 400, { error:"name and email are required" });
161
+
return;
162
+
}
163
+
164
+
sendJson(res, 201, { name, email });
165
+
} catch (error) {
166
+
sendJson(res, 400, { error:error.message });
167
+
}
168
+
169
+
return;
74
170
}
75
171
76
-
returnres.status(201).json({ name, email });
77
-
});
172
+
if (url.pathname==="/users") {
173
+
sendJson(res, 405, { error:"Method Not Allowed" });
174
+
return;
175
+
}
78
176
79
-
app.all("/{*splat}", (req, res) => {
80
-
res.status(405).json({ error:"Method Not Allowed" });
177
+
sendJson(res, 404, { error:"Not Found" });
81
178
});
82
179
83
-
app.listen(9000);
180
+
server.listen(9000);
84
181
```
85
182
86
-
Express 5 note: do not use bare `*` or `/*` here. Express 5 uses `path-to-regexp` with named wildcards, so `app.all("/{*splat}", ...)` is the safe catch-all form when you also need to match the root path `/`.
0 commit comments