Skip to content

Commit

Permalink
Merge pull request #3186 from myverdict/patch-9
Browse files Browse the repository at this point in the history
Update part4a.md
  • Loading branch information
mluukkai authored Oct 15, 2023
2 parents d41568c + 4e8f9c7 commit 38ca3e9
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/content/4/en/part4a.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,6 @@ The logger has two functions, __info__ for printing normal log messages, and __e

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.

The contents of the <i>index.js</i> file used for starting the application gets simplified as follows:

```js
const app = require('./app') // the actual Express application
const config = require('./utils/config')
const logger = require('./utils/logger')

app.listen(config.PORT, () => {
logger.info(`Server running on port ${config.PORT}`)
})
```

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.

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.

The handling of environment variables is extracted into a separate <i>utils/config.js</i> file:

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

The contents of the <i>index.js</i> file used for starting the application gets simplified as follows:

```js
const app = require('./app') // the actual Express application
const config = require('./utils/config')
const logger = require('./utils/logger')

app.listen(config.PORT, () => {
logger.info(`Server running on port ${config.PORT}`)
})
```

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.

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.

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.

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

0 comments on commit 38ca3e9

Please sign in to comment.