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!
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.
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.
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.
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/**
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With