Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js app structure, "folder by features" routing

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!

like image 317
endlessC Avatar asked Feb 12 '19 18:02

endlessC


People also ask

What is routes folder in node js?

Routing with Express in Node: Express. js has an “app” object corresponding to HTTP. We define the routes by using the methods of this “app” object. This app object specifies a callback function, which is called when a request is received. We have different methods in app object for a different type of request.

What is routing method in node js?

Routing refers to how an application's endpoints (URIs) respond to client requests. For an introduction to routing, see Basic routing. You define routing using methods of the Express app object that correspond to HTTP methods; for example, app.get() to handle GET requests and app.post to handle POST requests.

Which folder will serve as a controller in node js?

Next, create a folder called routes ; this is technically part of the controller, but it is nice to have all the routing in one, separate folder. Here is the loginController. js file in the controller folder: //js //For Register Page const registerView = (req, res) => { res.


1 Answers

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/**.

like image 138
Intervalia Avatar answered Oct 15 '22 15:10

Intervalia