Search code examples
javascriptnode.jsswaggerprojects-and-solutionsrestify

Where should I put algorithms for common endpoints in a Swaggerize-Restify project?


I've scaffolded a Restify project using the excellent generator-swaggerize and would like to know where to put common algorithms for API endpoint methods?

The algorithm maps database column names to API endpoint properties which I need to use for several endpoint handler methods which live in separate files according to their respective url structures: '/journeys', '/journeys/{id}' etc...

I'm assuming it would be best practice to add a helpers directory inside the /data directory as illustrated below which encapsulates common code for all journey endpoints? (Some directories removed for brevity)

│   api.json
│   package.json
│   README.md
│   server.js
├───config
│       swagger.json
├───data
│   │   db.js
│   │   db_constants.js
│   │   journeys.js
│   │
│   ├───journeys
│   │   │   {id}.js
│   │   │
│   │   ├───{id}
│   │         passengers.js
│   │   
│   │   
│   └──helpers

Solution

  • I'm don't use swagger so I'm not sure if they have a different idiom for this kind of thing, but it's fairly common practice to create a /lib directory at the root and then put your utility functions in there. You can then require your module from anywhere you need it.