Skip to content
This repository has been archived by the owner on Nov 22, 2019. It is now read-only.

Commit

Permalink
Modify the npm start script for running the app
Browse files Browse the repository at this point in the history
* package.json
 * Replace the development server start command with a cli start script

* start.sh
 * Can start server through either node or ng
 * Use npm start for production node execution
 * Use npm start -- -d for starting using 'ng serve'

* server.js
 * Scaffold the angular compiled artefacts in the dist/ directory as static
   files in express to allow them to be server through node.
  • Loading branch information
phantomjinx committed Jan 17, 2018
1 parent 4686e07 commit b276fa1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
6 changes: 5 additions & 1 deletion ngapp/README.md
Expand Up @@ -2,9 +2,13 @@

This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 1.3.2.

## Running with [Nodejs](https://nodejs.org)

Execute `npm start` to serve the Angular app using nodejs. Navigate to `http://localhost:8080`. This command is meant for production and the files in the `dist` directory will have to be recompiled for code changes to be displayed.

## Development server

Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
Execute `npm start -- -d` for a development server. Navigate to `http://localhost:8080/`. The app will automatically reload if you change any of the source files.

## Code scaffolding

Expand Down
3 changes: 2 additions & 1 deletion ngapp/package.json
Expand Up @@ -4,8 +4,8 @@
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve --host 0.0.0.0 --port 8080 --disable-host-check",
"build": "ng build",
"start": "./start.sh",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
Expand All @@ -23,6 +23,7 @@
"@angular/router": "^4.4.6",
"@swimlane/ngx-datatable": "^11.1.5",
"core-js": "^2.4.1",
"express": "^4.16.2",
"ng2-codemirror": "^1.1.3",
"ngx-bootstrap": "^1.9.3",
"patternfly": "^3.31.0",
Expand Down
15 changes: 15 additions & 0 deletions ngapp/server.js
@@ -0,0 +1,15 @@
var express = require("express");
var bodyParser = require("body-parser");

var app = express();
app.use(bodyParser.json());

// Create link to Angular build directory
var distDir = __dirname + "/dist/";
app.use(express.static(distDir));

// Initialize the app.
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
32 changes: 32 additions & 0 deletions ngapp/start.sh
@@ -0,0 +1,32 @@
#!/bin/bash

#################
#
# Show help and exit
#
#################
function show_help {
echo "Usage: $0 [-d] [-h]"
echo "-d: run in development mode"
echo ""
echo "To passthrough additional arguments, enter '--' followed by the extra arguments"
exit 1
}

#
# Determine the command line options
#
while getopts ":d" opt;
do
case $opt in
d) DEV=1 ;;
h) show_help ;;
esac
done
shift "$(expr $OPTIND - 1)"

if [ "${DEV}" == "1" ]; then
ng serve --host 0.0.0.0 --port 8080 --disable-host-check
else
ng build && node ./server.js
fi

0 comments on commit b276fa1

Please sign in to comment.