Skip to content

Commit 72fa072

Browse files
committed
added architecure details
1 parent efd6ee1 commit 72fa072

File tree

3 files changed

+64
-17
lines changed

3 files changed

+64
-17
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,5 @@ For example: it has not written about `const obj = new Object();` because we jus
6868
## Architecture guide
6969

7070
The software architecture (SA) is one of the most important parts of the developing. It helps developers easy to integrate into the ocean of the codes.
71-
The architecture describes a set of aspects and decisions of the software.
72-
This implies taking into consideration all kinds of requirements (performance, security, etc.), the organization of the system, how the system parts communicate with each other.\
73-
You should think about the long term, building software that is both functional right now and can support any sort of growth and change. \
71+
The architecture describes a set of aspects and decisions of the software. \
7472
[more](https://github.com/Vladinho10/node-server-template/blob/master/guides/architecture-guide.md)

guides/architecture-guide.md

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,31 @@ We'll speak about every small part of the code in detail even if it seems too pr
3636
1. [yarn.lock](#yarnlock)
3737

3838

39+
## Foreword
40+
41+
I'd like to start from naming of files or directories.
42+
1. All files or directories are started with lowercase. E.g. `configs`, `index.js`
43+
2. Complex words are written with dash. E.g. `admin-panel-ctrl`
44+
3. All directories are written with plural form. Exception is `dal` (data access layer), but in that dir again all subdirectories are in plural form.
45+
46+
In our software structure we use barrel export. A barrel is a way to rollup exports from several modules into a single convenient module. The barrel itself is a module file that re-exports selected exports of other modules. \
47+
Putting those words in action, is creating an index.js file to reexport everything the end user will need:
48+
```
49+
const objects = require('./objects');
50+
const strings = require('./strings');
51+
const general = require('./general');
52+
53+
module.exports = {
54+
objects,
55+
strings,
56+
general,
57+
};
58+
```
59+
Express framework v4.x doesn't use async/await functions, so we asyncified all we need (it is shown in `controllers` section).
3960

4061
## yarn.lock
4162

42-
If you use `npm` as your package manager, than equivalent to yarn.lock file is `package-lock.json` file.\
63+
If you use `npm` as your package manager, then equivalent to yarn.lock file is `package-lock.json` file.\
4364
Many developers think that yarn.lock file is unnecessary. ATTENTION ! :loudspeaker: Don't delete or add this file to `.gitignore`. It can bring really serious problems to your application.\
4465
In order to get consistent installs across machines, Yarn needs more information than the dependencies you configure in your package.json. Yarn needs to store exactly which versions of each dependency has installed.
4566
To do this Yarn uses a [yarn.lock](https://classic.yarnpkg.com/en/docs/yarn-lock/) file in the root of your project.
@@ -49,16 +70,16 @@ We'll speak about every small part of the code in detail even if it seems too pr
4970
## README
5071

5172
README.md file is an optional file. Absence of it doesn't leave a negative influence on your code. Although I strongly recommended you to write and complete your application 'introduction' file. It helps developers and visitors easily to understand what your app/repo is about.
52-
If you don't know how start to fill your README, or if you get lazy to do it, start from scratch and write only the most important things at first (say greetings to your visitors, how your app should be used, or how they can run it). When you already have some written some topics then you'll continue fill it again and again ).
73+
If you don't know how start filling your README, start from a scratch and write only the most important things at first (say greetings to your visitors, how your app should be used, or how they can run it). When you already have some written some topics then you'll continue fill it again and again ).
5374

5475
**[⬆ back to top](#table-of-contents)**
5576

5677
## package.json
5778

5879
As we all know the `package.json` is node.js app's backbone. \
59-
You should take care of it and keep clean. My advice is regularly check your all dependencies, dev-, peer- dependencies and so on. It's a good idea to keep updated your all modules, but you must be careful, your modules' version changes can entail many problems related with your system, node.js and their incompatibility. \
60-
When you usually install or remove a module, your package manager (yarn or npm) shows you a brief info about your modules (packages) states and statuses. They can be deprecated or can have vulnerabilities, warnings. You can follow yarn/npm advices and fixed them don't forgetting about the incompatibility. \
61-
You should regularly remove unused unnecessary modules from your app. There are many useful tools for it, e.g. [depcheck](https://github.com/depcheck/depcheck). You can install it globally and use for your all apps. \
80+
You should take care of it and keep clean. My advice is regularly check all of your dependencies, dev-, peer- dependencies and so on. It's a good idea to keep updated your all modules, but you must be careful, your modules' version changes can result in many problems related with your system, node.js and their incompatibility. \
81+
When you usually install or remove a module, your package manager (yarn or npm) shows you a brief info about your modules (packages) states and statuses. They can be deprecated or can have vulnerabilities, warnings. You can follow yarn/npm advices and fix them without forgetting about the incompatibilities. \
82+
You should regularly remove unused, unnecessary modules from your app. There are many useful tools for it, e.g. [depcheck](https://github.com/depcheck/depcheck). You can install it globally and use for your all apps. \
6283
In package.json you can add scripts for running, debugging, testing and so on.
6384

6485
**[⬆ back to top](#table-of-contents)**
@@ -77,14 +98,14 @@ We'll speak about every small part of the code in detail even if it seems too pr
7798

7899
## jenkinsfile
79100

80-
This file is use for auto deployment. It's an optional file. I'll add more info about this file after a while.
101+
This file is used for auto deployment. It's an optional file. I'll add more info about this file later.
81102

82103
**[⬆ back to top](#table-of-contents)**
83104

84105
## index
85106

86-
This is the entry point of your app. This is a cell from which is originated your application civilization. For running it, Node.js runs `index.js` at first. It initializes all classes and methods\
87-
Here you should write code few as possible. It should be a very simple and clean. Usually you create the server with a port and give connection to your database. You can also add here your routers and middlewares.
107+
This is the entry point of your app. This is a cell from which your application's civilization is originated . For running it, Node.js runs `index.js` at first. It initializes all classes and methods\
108+
Here you should write as less code as possible. It should be a very simple and clean. Usually you create the server with a port and give connection to your database. You can also add here your routers and middlewares.
88109

89110
**[⬆ back to top](#table-of-contents)**
90111

@@ -101,7 +122,7 @@ We'll speak about every small part of the code in detail even if it seems too pr
101122

102123
## .eslintrc
103124

104-
ESLint is a ESLint is a static code analysis tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs.
125+
ESLint is a static code analysis tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs.
105126
This is its config file. Our `.eslintrc` and style guide are being written according best practices of JS, Node.js (e.g. the great software development school of Airbnb), and, of course, our best modest experience :innocent: It was tested line by line. For more info please take a look our style guide.
106127

107128
**[⬆ back to top](#table-of-contents)**
@@ -130,7 +151,7 @@ We'll speak about every small part of the code in detail even if it seems too pr
130151
## views
131152

132153
This directory is used to save template engines. A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page.\
133-
You may not to have view engine. We decided to add it just for example. We use `EJS`. You can install another you wish. After it you need to set 2 things: the view engine and the directory for it (by default express use `views` dir). In our case we set them in entry `index.js` file.
154+
You may not have view engine. We decided to add it just for example. We use `EJS`. You can use any engine you prefer. After it you need to set 2 things: the view engine and the directory for it (by default express use `views` dir). In our case we set them in entry `index.js` file.
134155

135156
**[⬆ back to top](#table-of-contents)**
136157

@@ -143,14 +164,29 @@ We'll speak about every small part of the code in detail even if it seems too pr
143164
**[⬆ back to top](#table-of-contents)**
144165

145166
## services
146-
Services are conceptual parts of app architecture.
147-
<img src="../files/media/c-s-d.png" width="210" height="193" alt="some text"/>
167+
Services are conceptual parts of app architecture.
168+
<img src="../files/media/c-s-d.png" width="210" height="193" alt="c-s-d relation" align="right"/>
148169

149170

150171
**[⬆ back to top](#table-of-contents)**
151172

152173
## routers
153174

175+
A router is a method which forwards controllers (and middlewares) to the appropriate endpoint (route, path, url) and the appropriate http method.
176+
177+
`const userRt = require('express').Router();` // create a router for `users` resource
178+
`const { UserCtrl } = require('../controllers');` // import `users` resource's controller
179+
180+
`userRt.post('/v1/users', UserCtrl.post);` // for post request on `/v1/users` endpoint works UserCtrl's post method
181+
`userRt.get('/v1/users/:_id', UserCtrl.getOne);` // for get request on `/v1/users/:_id` endpoint works UserCtrl's getOne method
182+
183+
Routers directory's index.js file considers 3 parts.
184+
1. Middlewares
185+
1. Controllers
186+
1. ErrorHandler
187+
188+
At first, we run indexRouter's use method (which is a middleware too) which accepts combined middlewares group, then the router accepts controllers group by file name order and at last it accepts ErrorHandler middleware. \
189+
It should be noted that ErrorHandler accepts 4 arguments: `err, req, res, next`, but the last one is not used. If we don't put the 4th argument, Express will not understand the 1st argument as `err`, it will understand as `req`.
154190

155191
**[⬆ back to top](#table-of-contents)**
156192

@@ -161,16 +197,22 @@ Services are conceptual parts of app architecture.
161197

162198
## helpers
163199

200+
This is the place, where you can save reusable methods for different and recurring situations. Why? It's easy to reach to different data, when you already know where is it. Then it's comfortable use from one place and when you change some values it will change everywhere which has the reference to it.
201+
We separate helpers by data types' methods or for specific cases.
164202

165203
**[⬆ back to top](#table-of-contents)**
166204

167205
## guides
168206

169-
207+
This directory is intended to store guidelines related the project. In our case we have 2 of them.
208+
1. style guide (about syntax, semantics, manuscript of the code)
209+
1. architecture guide (current file)
210+
170211
**[⬆ back to top](#table-of-contents)**
171212

172213
## files
173214

215+
The `files` directory stores files related your template engine (.css, .js) or your media files and documents.
174216

175217
**[⬆ back to top](#table-of-contents)**
176218

@@ -181,12 +223,16 @@ Services are conceptual parts of app architecture.
181223

182224
## controllers
183225

226+
<img src="../files/media/app-architecture.png" alt="app-architecture"/>
227+
184228

185229
**[⬆ back to top](#table-of-contents)**
186230

187231
## constants
188232

189-
233+
Like `helpers`, these are meant to store global and local constants and enums.
234+
We separate constants by specific spheres and again use barrel export.
235+
190236
**[⬆ back to top](#table-of-contents)**
191237

192238
## configs

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
},
1212
"author": "Vlad Mkhitaryan",
1313
"license": "ISC",
14+
"engines": {
15+
"node": "12.x"
16+
},
1417
"dependencies": {
1518
"compose-middleware": "^5.0.1",
1619
"dotenv": "^8.2.0",

0 commit comments

Comments
 (0)