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
Copy file name to clipboardExpand all lines: config/source/skills/cloud-functions/SKILL.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,6 +82,8 @@ Use these rules whenever you are writing the function code itself:
82
82
- Do not write an HTTP Function as `exports.main(event, context)`. That is the Event Function contract.
83
83
- Treat the function as a standard web server process that must listen on port `9000`.
84
84
- With Node.js, prefer `http.createServer((req, res) => { ... })` by default so the runtime contract stays explicit.
85
+
- For Node.js HTTP Functions, choose one module system up front and keep it consistent. Default to CommonJS for simple functions (`require(...)`, no `"type": "module"` in `package.json`) unless you explicitly want ES Modules.
86
+
- If you do choose ES Modules (`"type": "module"` + `import ...`), do not mix in CommonJS-only globals or APIs such as `require(...)`, `module.exports`, or bare `__dirname`. In ESM, derive file paths from `import.meta.url` with `fileURLToPath(...)` only when needed.
85
87
- With the native `http` module, parse `req.url` yourself with `new URL(...)`, and read the request body from the stream before calling `JSON.parse`.
86
88
- Return responses explicitly with `res.writeHead(...)` and `res.end(...)`, including `Content-Type` such as `application/json; charset=utf-8` for JSON APIs.
87
89
- Keep routing and method handling explicit. Unknown paths should return `404`, and known paths with unsupported methods should normally return `405`.
Copy file name to clipboardExpand all lines: config/source/skills/cloud-functions/references/http-functions.md
+11Lines changed: 11 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,11 +99,22 @@ server.listen(9000);
99
99
100
100
- Do not write HTTP Functions as `exports.main = async (event, context) => {}`. That is the Event Function contract.
101
101
- Start an HTTP server explicitly with `http.createServer(...)` or a framework app, and always bind to port `9000`.
102
+
- Choose one Node.js module system and keep it consistent. For simple HTTP Functions, CommonJS is the safest default: use `require(...)` and leave `"type": "module"` out of `package.json`.
103
+
- If you intentionally use ES Modules, use `import ...` consistently and do not rely on CommonJS-only globals such as bare `__dirname`, `require(...)`, or `module.exports`. When you need the current file path in ESM, derive it from `import.meta.url`.
102
104
- 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
105
- Return JSON responses explicitly and set `Content-Type` yourself, for example `application/json; charset=utf-8`.
104
106
- 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
107
- Keep `scf_bootstrap`, `index.js`, `package.json`, and any bundled dependencies in the function directory that will be uploaded.
106
108
109
+
### Module system note
110
+
111
+
The minimal examples in this document use CommonJS:
112
+
113
+
-`const http = require("http")`
114
+
- no `"type": "module"` in `package.json`
115
+
116
+
That combination avoids the common ESM pitfall where `__dirname` is not defined. If you switch to ES Modules, switch the whole function to `import` syntax and update any file-path logic accordingly.
117
+
107
118
## Request handling rules
108
119
109
120
- With Node native `http`, use `new URL(req.url, "http://127.0.0.1")` and read `url.searchParams` for query values.
0 commit comments