Best practices · Stacks and tools
Node.js API Best Practices
Opens with creditsA practical guide to building and running Node.js APIs in production, checked against the Node.js docs, the Express production guides and the OWASP Node.js cheat sheet. It covers structure and config, validation and errors, async code, security, logging, testing, performance and shutdown.
What you will learn
7 chapters · about 18 min
1Project structure and configurationHow to lay out a Node.js API so it stays easy to change, and how to load and check configuration safely.4 practices · 3 min
2Validation and error handlingCheck every input at the edge, handle failures in one place, and keep internal details out of responses.4 practices · 3 min
- Async code and the event loopNode.js serves every request on one thread. These practices keep that thread free and your async code correct.4 practices · 2 minOpens with credits
- Security headers, rate limits and dependenciesBaseline protections every public Node.js API should have, drawn from the Express security guide and the OWASP Node.js cheat sheet.5 practices · 3 minOpens with credits
- Logging and observabilityHow to make a running API explain itself: structured logs, request ids, health checks and metrics.5 practices · 3 minOpens with credits
- TestingA test setup that catches real bugs: fast unit tests for logic, and integration tests through HTTP against a real database.3 practices · 2 minOpens with credits
- Performance, scaling and graceful shutdownSettings and patterns that keep the API fast under load and let you deploy without dropping requests.4 practices · 2 minOpens with credits
About this guide
These Node.js API best practices are for developers building REST or JSON APIs with Node.js, whether you use Express 5, Fastify 5 or another framework. They are checked against the official Node.js documentation, the Express production best practice guides and the OWASP Node.js Security Cheat Sheet, as of October 2026.
As of this review, Node.js 24 (Krypton) is the Active LTS line and Node.js 22 is in maintenance. Node.js 26 is scheduled to enter LTS on October 28, 2026, so plan to move to it in the months after that. Several features that used to need extra packages are now built in and stable, including loading .env files with --env-file, running TypeScript files through type stripping, and the node:test test runner.
The ideas in this guide are not tied to one framework. Validate every input at the edge. Handle errors in one place and never leak internals. Never block the event loop. Log in structured JSON. Shut down cleanly when your platform asks you to. Start with the first two chapters, structure and error handling, since they decide how easy the rest is to get right.
If you are building a backend project for your portfolio or a first job, this page doubles as a review list before you deploy it.
Common questions
Which Node.js version should I use in production in 2026?
Use an LTS line. As of October 2026, Node.js 24 is Active LTS and Node.js 22 is in maintenance until April 2027. Node.js 26 becomes LTS on October 28, 2026, so new projects can plan to move to it after that date. Avoid odd-numbered releases in production.
Is Express still a good choice for a Node.js API?
Yes. Express 5 is the current major line and handles rejected promises from async handlers automatically, which removes a common source of bugs. Fastify 5 is a strong alternative with built-in schema validation and logging. Either works well if you follow the practices on this page.
How do I handle errors in Express 5 async routes?
In Express 5 you can throw inside an async handler or let an awaited promise reject, and Express passes the error to your error-handling middleware as if you had called next(err). Define one error middleware with four arguments that maps known errors to status codes and returns a generic 500 for the rest.
Can Node.js run TypeScript without a build step?
Yes, through type stripping, which is on by default and stable since Node.js 24.12. Node removes the types but does not check them, ignores tsconfig.json, and does not support syntax like enums or decorators. Keep running tsc --noEmit in CI for type checking.
Do I still need the dotenv package?
Often not. Node.js can load environment files itself with node --env-file=.env, and the flag is no longer experimental since Node.js 24.10 and 22.21. Use --env-file-if-exists when the file may be missing, for example in production where variables come from the platform.
Sources (8)
Checked against these, last on 12 October 2026.
- Node.js release schedule, Node.jsgithub.com/nodejs/Release
- Modules: TypeScript, Node.js docsnodejs.org/api/typescript.html
- Command-line API, Node.js docsnodejs.org/api/cli.html
- Production Best Practices: Security, Expressexpressjs.com/en/advanced/best-practice-security.html
- Production best practices: performance and reliability, Expressexpressjs.com/en/advanced/best-practice-performance.html
- Health Checks and Graceful Shutdown, Expressexpressjs.com/en/advanced/healthcheck-graceful-shutdown.html
- Migrating to Express 5, Expressexpressjs.com/en/guide/migrating-5.html
- Nodejs Security Cheat Sheet, OWASPcheatsheetseries.owasp.org/cheatsheets/Nodejs_Security_Cheat_Sheet.html
Related on Sefism
Picked from the other libraries by shared topics.
Roadmaps
See allCheat sheets
See allBest practices
See all- Spring Boot Best PracticesBuild Spring Boot 4 services that are easy to change and safe to run: injection, configuration, REST layer, JPA, virtual threads, Actuator, tests and shutdown.
- ASP.NET Core Best PracticesBuild fast, safe ASP.NET Core 10 APIs: DI lifetimes, validated options, minimal APIs, problem details, async rules, EF Core, rate limits, tests and health checks.
- REST API Design Best PracticesDesign HTTP APIs clients enjoy using: resources and URLs, methods, status codes, problem details errors, pagination, idempotency keys, ETags, versioning and OpenAPI.
Checklists
See all- Production Readiness ChecklistCheck a service before it takes real traffic: ownership, SLOs, capacity, timeouts and retries, alerts, rollout and rollback, backups, security and on-call.
- API Security ChecklistCheck your REST or GraphQL API against the OWASP API Security Top 10: authentication, object authorization, rate limits, input validation, SSRF, CORS and logging.
- Database Migration ChecklistCheck a schema or data migration before it ships: expand and contract, lock behaviour, online indexes, batched backfills, rehearsal on real data, backups, rollback, deploy order and monitoring.
Project structures
See all- Express with TypeScript project structureFor students who know a bit of JavaScript and want a backend that looks like the ones teams actually run. Express gives you almost no structure, so this layout supplies it: one folder per feature under src/modules, each split into routes, controller, service and repository, with Zod at the edge and Vitest plus Supertest for tests.
- NestJS project structureFor people who want a structured TypeScript backend with clear rules about where code goes. Every feature is a Nest module that owns its controller, service, DTOs, entities and tests, so the app grows by adding folders, not by stretching old ones. The tree follows one orders API from nest new to a codebase a whole team can run for years.
- FastAPI project structureA typed Python API for an orders app, built the way the FastAPI docs and the official full stack template lay it out: one
apppackage, a router per feature, Pydantic schemas at the edges, services in the middle and SQLAlchemy models with Alembic migrations underneath. For students who have finished the FastAPI tutorial and want to know where the second, tenth and fiftieth file go.
Career questions
See all- Should I use Firebase or Supabase for my app?Both let you ship an app fast with auth, a database, storage and hosting-style services. Supabase is built on PostgreSQL, so it suits relational data and SQL skills you will reuse anywhere. Firebase is a mature Google platform with a document database and strong mobile tooling. Choose by your data shape, your platform, and how much you want to learn transferable database skills.
- Should I use PostgreSQL or MongoDB for my project?For most student and startup projects, PostgreSQL is the safer default: relational data, joins, constraints and transactions fit most apps, and it handles JSON and vector search too. MongoDB is a good fit when your data is naturally document-shaped and varies a lot, or your team already knows it well. Model your data first, then choose.
- How do I choose between frontend, backend, mobile, cloud, cybersecurity, data and AI without trying them all for years?Do not try to pick the best field; pick the one whose daily problems you can tolerate on a bad day. Run a short, cheap experiment in two or three candidates, compare what you enjoyed doing rather than what sounded exciting, check the local job market, and commit for six to nine months.
Stuck on something specific?
Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.
- 1:1 Mentorship
- Fresh Graduate Mentorship
- Career Guidance
Brand deals, promos and partnerships: tauseeffayyaz@gmail.com