Dynamic Configuration with the Config Library

Learn how to use the config library in Node and Express to dynamically configure application settings.

Currently, the port that our Express application is listening on is hard-coded to port 3000. In a production-like environment, this could be set to port 80 or for an application using secure communication to port 443, which is the default HTTPS port, or any other port, for that matter, if some kind of proxy server is being used.

Ideally, the port that this application is listening on should be controlled by a configuration file or a configuration setting of some sort instead of being hardcoded. We really should move settings like these into a configuration file of some sort that the application can consume.

Fortunately, setting and using configuration settings in an Express application is relatively simple, with the use of purpose-built libraries, such as the config library.

Using the config library

Using the config library, we can update our main.ts file as follows:

Get hands-on with 1400+ tech skills courses.