Skip to content

Commit 4e8f9c7

Browse files
author
Sam Vitare
authored
Update part4a.md
Reordered the notes to align the incorporation of code. Currently: logger.js --> index.js --> config.js Modified to: logger.js --> config.js --> index.js Modified the order as config.js is used in index.js and so it is important to show config.js before being used in index.js.
1 parent ffce937 commit 4e8f9c7

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/content/4/en/part4a.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,6 @@ The logger has two functions, __info__ for printing normal log messages, and __e
5454

5555
Extracting logging into its own module is a good idea in more ways than one. If we wanted to start writing logs to a file or send them to an external logging service like [graylog](https://www.graylog.org/) or [papertrail](https://papertrailapp.com) we would only have to make changes in one place.
5656

57-
The contents of the <i>index.js</i> file used for starting the application gets simplified as follows:
58-
59-
```js
60-
const app = require('./app') // the actual Express application
61-
const config = require('./utils/config')
62-
const logger = require('./utils/logger')
63-
64-
app.listen(config.PORT, () => {
65-
logger.info(`Server running on port ${config.PORT}`)
66-
})
67-
```
68-
69-
The <i>index.js</i> file only imports the actual application from the <i>app.js</i> file and then starts the application. The function _info_ of the logger-module is used for the console printout telling that the application is running.
70-
71-
Now the Express app and the code taking care of the web server are separated from each other following the [best](https://dev.to/nermineslimane/always-separate-app-and-server-files--1nc7) [practices](https://nodejsbestpractices.com/sections/projectstructre/separateexpress). One of the advantages of this method is that the application can now be tested at the level of HTTP API calls without actually making calls via HTTP over the network, this makes the execution of tests faster.
72-
7357
The handling of environment variables is extracted into a separate <i>utils/config.js</i> file:
7458

7559
```js
@@ -92,6 +76,22 @@ const config = require('./utils/config')
9276
logger.info(`Server running on port ${config.PORT}`)
9377
```
9478

79+
The contents of the <i>index.js</i> file used for starting the application gets simplified as follows:
80+
81+
```js
82+
const app = require('./app') // the actual Express application
83+
const config = require('./utils/config')
84+
const logger = require('./utils/logger')
85+
86+
app.listen(config.PORT, () => {
87+
logger.info(`Server running on port ${config.PORT}`)
88+
})
89+
```
90+
91+
The <i>index.js</i> file only imports the actual application from the <i>app.js</i> file and then starts the application. The function _info_ of the logger-module is used for the console printout telling that the application is running.
92+
93+
Now the Express app and the code taking care of the web server are separated from each other following the [best](https://dev.to/nermineslimane/always-separate-app-and-server-files--1nc7) [practices](https://nodejsbestpractices.com/sections/projectstructre/separateexpress). One of the advantages of this method is that the application can now be tested at the level of HTTP API calls without actually making calls via HTTP over the network, this makes the execution of tests faster.
94+
9595
The route handlers have also been moved into a dedicated module. The event handlers of routes are commonly referred to as <i>controllers</i>, and for this reason we have created a new <i>controllers</i> directory. All of the routes related to notes are now in the <i>notes.js</i> module under the <i>controllers</i> directory.
9696

9797
The contents of the <i>notes.js</i> module are the following:

0 commit comments

Comments
 (0)