eolas/Programming_Languages/NodeJS/Architecture/Ports.md

20 lines
688 B
Markdown
Raw Normal View History

2022-04-23 13:26:53 +01:00
---
tags:
- Programming_Languages
- backend
- node-js
- node-modules
---
2022-07-12 19:30:03 +01:00
# Ports
2022-04-23 13:26:53 +01:00
When working in development we are able to specify the specific port from which we want top serve our application. In production, we do not always have this control: the port will most likely be set by the provider of the server environment.
2022-07-14 08:00:04 +01:00
While we may not know the specific port, whatever it is, it will be accessible via the `PORT` environment variable. So we can use this when writing our [event listeners](Events%20module.md#event-emitters):
2022-04-23 13:26:53 +01:00
````js
const port = process.env.PORT || 3000;
````
This way, if a port is set by the provider it will use it. If not, it will fall back to 3000.