Skip to content

Commit 2e248d8

Browse files
committed
allow to set the server port via environment variable PORT
1 parent 5e23470 commit 2e248d8

5 files changed

Lines changed: 40 additions & 3 deletions

File tree

changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ application, major version changes in Plotly.js will also trigger a major
1111
version change in this application.
1212

1313

14+
## Version 1.5.0-pre1 (2021-08-01)
15+
16+
* __[new feature]__
17+
The port to which the server binds can now be changed by setting the environment
18+
variable `PORT`. If `PORT` is not set or is not a valid port number, then the
19+
default port 3000 is used.
20+
1421
## Version 1.4.2 (2021-08-01)
1522

1623
* __[maintenance]__

export-server/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

export-server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "plotly-node-export-server",
3-
"version": "1.4.2",
3+
"version": "1.5.0-pre1",
44
"description": "Plotly.js Node.js export server",
55
"repository": {
66
"url": "https://gitlab.com/striezel/plotly-node-export-server.git",

export-server/server.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,19 @@ const url = require('url');
2727
const uuidv4 = require('uuid/v4');
2828

2929
const hostname = 'localhost';
30-
const port = 3000;
30+
// Use port from environment variable PORT, if it is set and valid.
31+
const parsedPort = parseInt(process.env.PORT);
32+
if (process.env.PORT) {
33+
if (isNaN(parsedPort)) {
34+
console.log('Warning: PORT environment variable is not a number, using port 3000!');
35+
} else if (parsedPort <= 0 || parsedPort > 65535) {
36+
console.log('Warning: PORT environment variable is not a number between 1 and 65535, using port 3000!');
37+
} else if (parsedPort < 1024) {
38+
// Unless user is root or admin, binding to well-known ports is usually denied by the OS.
39+
console.log('Warning: Binding to ports below 1024 may require root / administrative privileges!');
40+
}
41+
}
42+
const port = (parsedPort && parsedPort > 0 && parsedPort < 65536) ? parsedPort : 3000;
3143

3244
// ** Preparation, step 1: **
3345
// Find the PhantomJS executable. It is usually located in the node_modules

readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ You can simply start the application via
5151
which fires up the Node.js application. The server will then listen on
5252
<http://localhost:3000/> for incoming connections.
5353

54+
If you want the server to listen on a different port, then you can set the
55+
environment variable `PORT` accordingly. On Linux-like systems you can do
56+
57+
``` bash
58+
export PORT=4000
59+
npm start
60+
```
61+
62+
The equivalent on Windows command prompt would be
63+
64+
``` cmd
65+
SET PORT=4000
66+
npm start
67+
```
68+
69+
In these cases the server will bind to port 4000 instead of the default port
70+
3000.
71+
5472
## Usage
5573

5674
To generate a PNG file of a Plotly plot, just send an HTTP POST request to the

0 commit comments

Comments
 (0)