1.

Explain some of the best practises in Express JS?

Answer»

Express app doesn’t follow any defined structure, so sometimes it’s up to the developer or the Architect to create one. But when we don’t follow a structure, it is very difficult to maintain the project at the later STAGES when it grows. 

There are two types of Express projects. One which has the complete frontend and backend BUILT with some templating engine. The other, and more popular one, is to have only RESTful routes built in Express, and a separate frontend which is generally ReactJS, Angular or Vue. 

We will look into the structure of a complete web app with express first.  

Here, we will have a config folder, which will contain all of our configuration details. It will have a different file for general config, one for credentials and one for database configurations. 

After that we will have our models folder which will contain the structure of the different data used in our project. 

Next, we will have a public folder. This folder will have the images, javascript and css folders which will contain the respective files. 

Next, we will have the routes folder, which contains our different routes used in the project. 

We will also have a views folder containing our template files like pug or EJS. These files get converted into actual HTML files during runtime and shown in the client browser. 

Finally, we will have the app.js files which is the entry point and connect to everything. We will also have a routes.js file, which is the CENTRAL routing file and CONNECTS to the routes in the routes folder. 

The RESTful API design is much simpler and won’t contain the views and public folders, because we are not creating the frontend in it.  

We can generate these structures automatically by using any express GENERATOR package, while starting the project.  



Discussion

No Comment Found