Node.js app structure, "folder by features" routing
Asked Answered
V

1

7

I'm making a scalable REST-API, but I barely find articles on advanced node.js application structures in terms of a big application, since most of them are using simple starter projects.

I'm using the "folder-by-feature" structure, based on this article and this answer.

My question: what is the better structure of the solutions bellow?

1. Keep the routes in a separate folder:

src
  product
    index.js
    product.spec.js
  routes
    index.js
    product.js
    user.js
  user
    index.js
    user.spec.js

2. Put the route into its corresponding folder:

src
  product
    index.js
    product.route.js
    product.spec.js
  user
    index.js
    user.route.js
    user.spec.js

Using the routes in the index.js files.

Are there any better solutions?

Any article about advanced, scalable node project structures would be appreciated!

Vinson answered 12/2, 2019 at 18:6 Comment(3)
In my opinion, the concept of "big application" is a nonsense, you should always think that your app will be "big" at the end. What is better depends on you I'm afraid.Valletta
That's true, but I wanted to highlight that it's not about some simple example project. I'm looking for someone who has previously built a similar structure, because as the project grows you can see the advantages and disadvantages of each option.Vinson
github.com/rahulshukla-rs/…Dustpan
B
1

Since this is an opinion question here is mine:

My build migrates everything from src to dist. Some is compiled and some is just copied. I then run directly from the dist folder.

src
  api
    <api files/folders>
  lib
    <common lib files/folders>
  routes
    <Route files (app.use, app.get, etc.)>
  static
    <static css, images, script, etc.>
    <I do not include src code that is compiled in any way>
  ui
    <LESS, SASS, JS, etc that will be compiled, combined, packed, etc>
  views
    <ejs files>
  app.js

Things in src/ui/** get compiled and placed into dist/static/**.

Brighten answered 12/2, 2019 at 18:21 Comment(3)
I see you prefer using routes in a separate folder. Do you think migration is need for an API which does not contain client-side code?Vinson
I have my own API system, for rest endpoints on Node, that loads all files in the /dist/api folder and auto-generates all of the app.get, app.post, app.delete, etc commands. Everything I run is run from the dist folder. In fact we only deploy the dist folder, node_modules folder and package.json Nothing else is deployed.Brighten
Thank you, migrating to a dist folder seems like a good way for deployment.Vinson

© 2022 - 2024 — McMap. All rights reserved.