Skip to content

Commit

Permalink
fix: custom port args
Browse files Browse the repository at this point in the history
  • Loading branch information
an-parubets committed Jun 2, 2022
1 parent 6cfbd0c commit 6bed0bd
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 19 deletions.
1 change: 0 additions & 1 deletion .env

This file was deleted.

14 changes: 0 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
]
},
"dependencies": {
"dotenv": "^16.0.1",
"express": "^4.18.1",
"morgan": "^1.10.0"
},
Expand Down
11 changes: 11 additions & 0 deletions src/config/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const argv = require('../utils/argv');

const DEFAULT_PORT = 8000;

const portFromArguments = argv('port');

if (portFromArguments) {
process.env.PORT = portFromArguments;
} else {
process.env.PORT = DEFAULT_PORT;
}
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#! /usr/bin/env node

require('dotenv').config();
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', (err) => {
throw err;
});

require('./config/env');

const express = require('express');
const logger = require('./logger');
const logger = require('./utils/logger');

const PORT = process.env.npm_config_port ?? process.env.PORT;
const PORT = process.env.PORT;

const app = express();

Expand Down
15 changes: 15 additions & 0 deletions src/utils/argv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const process = require('process');

const argv = (key) => {
// Return true if the key exists and a value is defined
if (process.argv.includes(`--${key}`)) return true;

const value = process.argv.find((element) => element.startsWith(`--${key}=`));

// Return null if the key does not exist and a value is not defined
if (!value) return null;

return value.replace(`--${key}=`, '');
};

module.exports = argv;
File renamed without changes.

0 comments on commit 6bed0bd

Please sign in to comment.