2022-04-23 13:26:53 +01:00
|
|
|
---
|
2022-09-06 15:44:40 +01:00
|
|
|
categories:
|
|
|
|
- Programming Languages
|
2022-04-23 13:26:53 +01:00
|
|
|
tags:
|
|
|
|
- backend
|
|
|
|
- node-js
|
|
|
|
---
|
|
|
|
|
2022-07-12 19:30:03 +01:00
|
|
|
# Ports
|
2022-04-23 13:26:53 +01:00
|
|
|
|
2024-02-02 15:58:13 +00: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-04-23 13:26:53 +01:00
|
|
|
|
2024-02-02 15:58:13 +00: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
|
|
|
|
2022-09-06 15:44:40 +01:00
|
|
|
```js
|
2022-04-23 13:26:53 +01:00
|
|
|
const port = process.env.PORT || 3000;
|
2022-09-06 15:44:40 +01:00
|
|
|
```
|
2022-04-23 13:26:53 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
This way, if a port is set by the provider it will use it. If not, it will fall
|
|
|
|
back to 3000.
|