Skip to content

Commit

Permalink
Use NODE_ENV to detect production/development (#239)
Browse files Browse the repository at this point in the history
Following the changes to the CLI in pull request #233 ("use a new
library to parse command line args"), keep HTTPS enabled by default on
production while disabling it by default on development, so that
developers can simply run `npm start` instead of `npm start -- -i`.
  • Loading branch information
psvenk authored Jan 5, 2021
1 parent ba93760 commit bc04c54
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ downloaded Aspine, and run `npm install`.
+ On Windows, double-click on the script `npminstall.bat`. The `.bat` file
extension may be invisible depending on your system configuration.

- Start the Aspine server by running `npm start -- -i` in a terminal window (or
- Start the Aspine server by running `npm start` in a terminal window (or
Command Prompt on Windows). If you are on a Unix-like system, you can run
`npm start -- -i &` to run the server in the background and then run
`npm start &` to run the server in the background and then run
`killall node` when you want to kill the server.

## Authors
Expand Down
2 changes: 1 addition & 1 deletion restart-serve.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
sudo pkill "node"
sudo $NVM_BIN/npm start &
sudo NODE_ENV=production $NVM_BIN/npm start &
23 changes: 19 additions & 4 deletions serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,25 @@ const version = child_process.execSync('git describe')

program
.version(version)
.option('-i, --insecure', 'do not secure connections with TLS (HTTPS)')
.option('-p, --port <number>', 'port to listen on', 8080)
.option('-P, --port-https <number>', 'port to listen on for HTTPS', 4430)
.parse();
.option('-P, --port-https <number>', 'port to listen on for HTTPS', 4430);

// Secure by default on production, insecure by default for development
if (process.env.NODE_ENV === "production") {
program.option('-i, --no-secure, --insecure',
'do not secure connections with TLS (HTTPS)'
);
program.option('-s, --secure',
'secure connections with TLS (HTTPS) [default for production]'
);
} else {
program.option('-s, --secure', 'secure connections with TLS (HTTPS)');
program.option('-i, --no-secure, --insecure',
'do not secure connections with TLS (HTTPS) [default for development]'
);
}

program.parse();

// ------------ Web Server -------------------
const app = express();
Expand All @@ -29,7 +44,7 @@ app.listen(program.port, () =>
console.log(`Aspine listening on port ${program.port}!`)
);

if (!program.insecure) {
if (program.secure) {
app.all('*', (req, res, next) => {
if (req.secure) {
return next();
Expand Down
3 changes: 0 additions & 3 deletions start-local.sh

This file was deleted.

0 comments on commit bc04c54

Please sign in to comment.