{"success":true,"name":"separate-web-layer-from-business-logic-in-node-js-services","description":"Node.js route handlers growing fat with logic and DB calls.","category":"ai-brimind","path":"ai-brimind/separate-web-layer-from-business-logic-in-node-js-services/SKILL.md","file":"SKILL.md","content":"---\nname: separate-web-layer-from-business-logic-in-node-js-services\ndescription: \"Node.js route handlers growing fat with logic and DB calls.\"\n---\n\n# Separate Web Layer From Business Logic in Node.js Services\n\n## When to use\nProject is a Node.js backend, and route handlers mix HTTP parsing, business rules, and database queries in one function.\n\n## Example request\nA 200-line Express handler doing validation, rules, and Mongo queries → untestable without HTTP; layered architecture (route → service → data) separates them. Business rule duplicated in two endpoints → it lives in the web layer so it can't be shared — move it to a service function both call. Testing logic requires spinning up the server → logic welded to req/res; pure service functions test with plain calls. Swapping REST for a queue consumer means rewriting rules → rules trapped in transport code — transport-agnostic services survive the swap.\n\n## Instructions\nAct as a backend architect who keeps HTTP at the edges and business logic in the middle. Enforce the layering: route handlers only parse/validate input, call ONE service function, and shape the response — they never contain business rules or direct database queries. Service functions hold the rules, take plain typed arguments (never req/res objects — passing those welds logic to the transport), and call data-access functions for persistence. The test of the boundary: could this service function be called from a CLI script, a queue consumer, or a test with zero HTTP involved? If not, transport has leaked in. When you find a fat handler, extract in that order: validation to a schema at the top, rules to a service, queries to a data layer — and when the same rule appears in two handlers, that duplication is the signal the service layer is missing. Don't over-layer trivial CRUD into five files either; the goal is separated responsibilities, not ceremony. Only finish once each layer is confirmed to have a single reason to change, and explain what each layer owns.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #997\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}