REST API directory structure

Arun Rajeevan
1 min readOct 2, 2019

--

After consuming so many hours with developing apis in Nodejs, using apis and modifying existing apis I feel that there has to be a cleaner directory structure to help a new person in the team to understand the code straightaway.

Root directory:

|-app.js
|-config/
|-swagger.yml
|-controllers/
|-apiSchemas/
|-routes.js
|-businessLogic/
|-externalAPIs/
|-logger.js
|-tracer.js
|-middlewares/
|-test/
|-package.json
|-.env
|-readme

Individual directories:

config
|-app.js
|-constants.js
|-externalAPI1.js
|-externalAPI2.js

externalAPIs
|-externalAPIName1/
|-externalAPIName2/

externalAPIs/externalAPIName1
|-schema.js
|-errorMapper.js
|-contractMapper.js

controllers
|-operationId1.js
|-operationId2.js

Note : operation id is a field in swagger.yml that gets appended to req object if we use a swagger middleware

businessLogic
|-operationId1.js
|-operationId2.js

apiSchemas
|-operationId1.js
|-operationId2.js

Note: apiSchema validates the input request body.

middlewares
|-errorHandler.js

tests
|-unit
|-integration

--

--

No responses yet