Skip to content

Commit 9c848db

Browse files
author
CodeBuddy Attribution Bot
committed
fix(attribution): Skill 文档未清晰说明 HTTP 云函数的代码编写规范 (issue_mns1xuxr_arrdn2)
1 parent 97bd118 commit 9c848db

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

config/source/skills/cloud-functions/SKILL.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ Use these rules whenever you are writing the function code itself:
8282
- Do not write an HTTP Function as `exports.main(event, context)`. That is the Event Function contract.
8383
- Treat the function as a standard web server process that must listen on port `9000`.
8484
- 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.
8587
- With the native `http` module, parse `req.url` yourself with `new URL(...)`, and read the request body from the stream before calling `JSON.parse`.
8688
- Return responses explicitly with `res.writeHead(...)` and `res.end(...)`, including `Content-Type` such as `application/json; charset=utf-8` for JSON APIs.
8789
- Keep routing and method handling explicit. Unknown paths should return `404`, and known paths with unsupported methods should normally return `405`.

config/source/skills/cloud-functions/references/http-functions.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,22 @@ server.listen(9000);
9999

100100
- Do not write HTTP Functions as `exports.main = async (event, context) => {}`. That is the Event Function contract.
101101
- 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`.
102104
- 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`.
103105
- Return JSON responses explicitly and set `Content-Type` yourself, for example `application/json; charset=utf-8`.
104106
- 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.
105107
- Keep `scf_bootstrap`, `index.js`, `package.json`, and any bundled dependencies in the function directory that will be uploaded.
106108

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+
107118
## Request handling rules
108119

109120
- With Node native `http`, use `new URL(req.url, "http://127.0.0.1")` and read `url.searchParams` for query values.

0 commit comments

Comments
 (0)