You can create controller for each route file to better manage your code. The routes have it's own folder in the root directory of a express project but where does the controllers belong?
I have seen that they have an own folder named "controllers" in the root directory too but I have also seen that each router has his own folder where router.js and index.js (the controller) are.
But what is the right folder structure for controllers for express js projects?
Ohk, just don't get too much confused. Organizing the files and folders totally depends on you, Express or node doesn't force you to maintain specific structure. In general developers like to keep things like below:
.
├── config # App configuration files
│ ├── sequalize.json # Sequalize config
│ ├── serviceOne.json # ServiceOne config
│ └── ... # Other configurations
├── routes
│ ├── controllers # Request managers
│ ├── middlewares # Request middlewares
│ └── routes.js # Define routes and middlewares here
├── services # External services implementation
│ ├── serviceOne
│ └── serviceTwo
│ └── ... # Other services
├── db # Data access stuff (Sequalize mostly)
│ ├── models # Models
│ ├── migrations # Migrations
│ ├── seeds # Seeds
│ └── index.js # Sequalize instantiation
├── core # Business logic implementation
│ ├── accounts.js
│ ├── sales.js
│ ├── comments.js
│ └── ... # Other business logic implementations
├── utils # Util libs (formats, validation, etc)
├── tests # Testing
├── scripts # Standalone scripts for dev uses
├── pm2.js # pm2 init
├── shipitfile.js # deployment automation file
├── package.json
├── README.md
└── app.js # App starting point
For more details you can refer https://medium.com/codebase/structure-of-a-nodejs-api-project-cdecb46ef3f8
If you have an exigence to implement your server-side project with Express js follow the answer below , or I suggest for you Nest.js
Nest (NestJS) is a framework for building efficient, scalable Node.js™ server-side applications
It has his own structure (services, controllers, routers ) well organized, and implementing good practices for server-side projects, take a look
It all depends on what you want, express don't have a "you need to use this format or else your api will surely fail" rule.
If you have an experience with using a PHP framework before, you can follow their MVC project structure if you want to or you can follow this nodejs best practices.
Related
I'm new to Server-Side Rendering and I'm wondering how do I apply best practices, separation of concern and naming convention for SSR, CSR and REST API code. Currently my folder structure looks like this:
.
├── client
│ ├── src
│ └── webpack.config.js - // This is the config for CSR
├── routes
│ ├── api
│ │ ├── controllers
│ │ │ ├── users.controller.js
│ │ └── routing
│ │ └── users.route.js
│ └── dao
│ └── usersDAO.js
├── app.js
├── server.js
├── webpack.client.js
└── webpack.server.js - // This one uses the app.js as an entry point
The REST API is implemented in the /server.js, the SSR routes are in my /app.js, and in my /server.js I have a conditional statements that uses environment variable and webpack's global constants to determine whether I'm using SSR or CSR, if so, server.js will then not listen on the server nor connect to the database, and instead just initialize the routes and the listen method and database connection are instead invoked in the /app.js rather than server.js. Is this a good practice or is there any better approach for separating these routes? Should I also make a folder and separate out each SSR routes? Also, how exactly does the folder structure of the separation of controllers, routes and DAO would look like?
Question
Is it possible to configure a global, custom entry point to be used by require for all subfolders in a Node.js project?
Rationale
When working in Node.js, I like having my index.js file as the topmost file in each subfolder in my IDE.
However, depending on the IDE and the way it sorts files, this is not always possible (for example, VSCode has several sorting options available, and none of them can achieve this).
To achieve that, I prefix it with _index.js, but then lose the built-in capability of require to recognize it as the default entry point.
Although this can be mitigated by adding a package.json into each subfolder, with a main property directing to the entry point file - I'd like to know if there's a way to define a "global" custom entry point, be it in the topmost package.json or using some npm package which I'm not aware of.
Example
Let's say I have the following folders structure, and assume that our IDE sorts files alphabetically:
MyApp
├── app.js
├── package.json
├─┬ featureA
│ ├── func1.featureA.js
│ ├── func2.featureA.js
│ └── index.js
└─┬ featureB
├── func1.featureB.js
├── func2.featureB.js
└── index.js
To keep index.js as the topmost file, we prefix it with an underscore, and use a package.json for each subfolder to define it as an entry point:
MyApp
├── app.js
├── package.json
├─┬ featureA
│ ├── _index.js
│ ├── func1.featureA.js
│ ├── func2.featureA.js
│ └── package.json
└─┬ featureB
├── _index.js
├── func1.featureB.js
├── func2.featureB.js
└── package.json
The package.json for both featureA and featureB is identical:
{
"main": "_index.js"
}
That package.json is necessary so that we can use require in the following way in app.js:
// app.js
const featureA = require('./featureA');
const featureB = require('./featureB');
But can these two package.json files be replaced with some "global" alternative?
What is best folder structure for a project that includes:
2 database (mongodb, influxdb)
socket io
...
It all depends on the personal preferences. Make your folder structure the way you are most comfortable working with.
For example, I love my code to be split in submodules or simply said to be in different directories, accordingly to the job the code is used for.
I'd personally go with a couple of folders:
.{src}
├── controllers # All controller operations according the routes are stored here
│ ├── authController.ts # Handles authentication requests
│ ├── usersController.ts # Handles users route requests
│ └── ...
│
├── database # All database connections are stored here. For example you have two databases
│ ├── db.ts # Initialize DB connection
│ └── ...
│
├── middleware
│ ├── authenticated.ts # Decode and verify JWT token
│ ├── error.ts # Common Error Handler
│ ├── logger.ts # Control logging levels
│ └── ...
│
├── models # Simple descriptor of the database tables
│ ├── usersModel.ts # DB model for users
│ └── ...
│
├── schema # Schemas that are used for CRUD operations with the models
│ ├── users.ts # DB Schema for users
│ └── ...
│
├── listeners
│ ├── socketsManager.ts # Socket listeners/emitters handle
│ └── ...
│
├── app.ts # Entry file for the project
├── .env # Store environment variables
├── routes.ts # All routes initializer
└── ...
Although, you might not like the naming or the order I've put on, you can create your own one you feel comfortable with. Another thing you might do is take a look at some conventions that are up on the internet or some of the most popular projects like frameworks.
It's all depending on your needs and what you feel comfortable working with, after all.
In the ssr vue (server side rendering for vuejs) documentation there's a code structure example containing a webpack build step:
https://ssr.vuejs.org/en/structure.html
The structure looks like this:
src
├── components
│ ├── Foo.vue
│ ├── Bar.vue
│ └── Baz.vue
├── App.vue
├── app.js # universal entry
├── entry-client.js # runs in browser only
└── entry-server.js # runs on server only
I am missing a main template(in rails or expressjs lingo a layout) which is supposed to contain the
<!--vue-ssr-outlet-->
marker.
Or am I missing something?
In SSR there is not a layouts folder. Your template (default) is in your App.vue file.
I am new to Node.js application development with expressjs framework.
I created a skeleton with expressjs-generator.
This skeleton have following directories and files:
.
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ ├── images
│ ├── javascripts
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
└── views
├── error.jade
├── index.jade
└── layout.jade
After it: I use the following command to run this application.
set debug=myapp:* & start npm
Now this is successfully running at Port 3000
This shows the homepage with Express Welcome message.
I want to make change in Homepage of my application. How it can be possible?
You can do that by changing the index.jade as #brute_force mentioned. If you are not familiar with jade, you can also add a index.html in the public folder and update the index.html instead.