AI Skill Report Card
Applying Alibaba Node.js Conventions
Quick Start13 / 15
Apply these conventions when writing or reviewing Node.js code:
JavaScript// ✅ Good: destructure built-in globals, promises API, proper import order, native Error const fs = require('fs').promises; const http = require('http'); const moment = require('moment'); const Car = require('./models/car'); async function readConfig(path) { try { return await fs.readFile(path, 'utf8'); } catch (err) { throw err; // never throw a literal } }
Supported Node.js versions: Current, Active LTS, Maintenance LTS only. Never target Unstable/EOL versions, and avoid odd-numbered versions (19, 21, 23) in production — their support window is only 6 months.
Recommendation▾
Add a bad-example counterpart for the Quick Start snippet to reinforce good vs bad contrast (currently only shows good code with inline comments).
Workflow14 / 15
When reviewing/writing Node.js code, check in this order:
Progress:
- [ ] 1. Verify Node.js version target (LTS/Current only, avoid odd versions)
- [ ] 2. Check coding style (globals, promises, import order, error throwing, no sync I/O)
- [ ] 3. Check security rules (error leakage, headers, JSONP, auth, SQL injection, uploads, redirects, input validation)
- [ ] 4. Check best practices (statelessness, static files, CPU-bound tasks, async/await, monitoring)
- [ ] 5. Recommend eslint-config-ali/node (or /typescript/node) for automated enforcement
1. Coding Style
- Prefer built-in globals over
require-ing them explicitly where Node exposes globals (Buffer,URL,URLSearchParams,TextEncoder,TextDecoder,process,console). ESLint:node/prefer-global. - Prefer built-in promise APIs over callback style:
require('dns').promises,require('fs').promises(Node ≥ v11.14.0). ESLint:node/prefer-promises. - Import order (ESLint:
import/order), each group separated by a blank line, alphabetical within group (destructured imports sorted by first named import, and internally):- Node built-in modules
- npm packages
- Local files (relative paths)
- Throw only
Errorobjects/instances, never literals, strings, orundefined. ESLint:no-throw-literal. - Never use sync methods (
fs.readFileSync,child_process.execSync, etc.) in production code paths — they block the event loop. Usefs.promisesor a library likemz.
2. Security Rules (mandatory unless noted "recommended")
| Rule | Requirement |
|---|---|
| Error exposure | Hide detailed error info/stack traces from clients |
| Framework fingerprint | Hide or fake X-Powered-By and other stack-identifying headers |
| JSONP | Strictly validate request origin via a domain whitelist |
| Sensitive lookups | Never trust user identity from query params or plaintext cookies for sensitive queries (prevents unauthorized/privilege-escalation access) |
| SQL injection | Use prepared statements for any SQL containing user input; escape/whitelist input used for table/column names |
| Dependencies (recommended) | Regularly audit for outdated/vulnerable deps and upgrade |
| File uploads (recommended) | Never store user uploads on local disk — upload to OSS/object storage |
| Redirects (recommended) | Whitelist target domains before redirecting based on user input |
| Input validation (recommended) | Validate all request input using jsonschema or joi |
3. Best Practices
- Stateless services: use external stores; killing an instance must not lose data.
- Don't serve static files from Node — offload to CDN to avoid blocking other request handling.
- Delegate CPU-intensive work (gzip, TLS termination) to a reverse proxy (nginx) rather than doing it in Node.
- Prefer
async/awaitover raw callbacks/promise chains to avoid callback hell and simplify error handling. - Use
util.promisifyto convert callback-style APIs:JavaScriptconst util = require('util'); const fs = require('fs'); const stat = util.promisify(fs.stat); - Use native
Promise, not third-party libraries likebluebird. - Return
thisfrom instance methods to enable method chaining:JavaScriptclass Jedi { jump() { this.jumping = true; return this; } setHeight(h) { this.height = h; return this; } } - Use a Node.js performance monitoring tool (e.g., Alibaba Cloud Node.js Performance Platform) for APM, heap snapshots, dependency/security alerts, and slow-HTTP/error logging.
Recommendation▾
Include an ESLint config snippet or link showing how to actually wire up eslint-config-ali/node, since it's recommended but not demonstrated.
Examples15 / 20
Example 1 — Import order violation Input:
JavaScriptconst Car = require('./models/car'); const moment = require('moment'); const fs = require('fs');
Output:
JavaScriptconst fs = require('fs'); const moment = require('moment'); const Car = require('./models/car');
Example 2 — Sync I/O in a request handler Input:
JavaScriptapp.get('/file', (req, res) => { const data = fs.readFileSync('./somefile', 'utf-8'); res.send(data); });
Output:
JavaScriptconst fs = require('fs').promises; app.get('/file', async (req, res) => { const data = await fs.readFile('./somefile', 'utf-8'); res.send(data); });
Example 3 — Throwing a literal
Input: throw 'Invalid input';
Output:
JavaScriptthrow new Error('Invalid input');
Recommendation▾
Expand security examples with concrete before/after code (like the SQL injection or JSONP whitelist patterns) rather than only describing them in a table.
Common Pitfalls
- Don't leak stack traces or internal error messages to API clients.
- Don't leave
X-Powered-Byor similar headers exposing the framework. - Don't build SQL by string concatenation with user input — always use prepared statements.
- Don't accept identity from query params/plaintext cookies for authorization decisions.
- Don't use
fs.*Sync/child_process.*Syncin server request paths. - Don't target Node.js Unstable or EOL versions, or odd-numbered releases in production.
- Don't mix import ordering arbitrarily — group and alphabetize consistently.
- Don't reach for
bluebirdor similar when nativePromisesuffices.