This is a Dingo (Australischer Windhund) |
This example shows:
- how to use Netbeans for working with this HTML5 project
- how to use Gulp.js to build the Angular 2 TypeScript project
- how to create and run a Web-Server based on Node.js Express
- how to use TypeScript as source file language
- how to use option include in file tsconfig.json
- how to use an Angular 2 application supported by this Web-Server
Follow the links to find some more information for Netbeans IDE, Gulp.js, Node.js, Express Web-Server, Angular 2 and TypeScript.
Netbeans IDE and the following plugins should be installed:
- HTML5 Kit
- TypeScript Editor http://plugins.netbeans.org/plugin/60605/typescript-editor
- Markdown Support http://plugins.netbeans.org/plugin/50964/markdown-support
Make sure that an actual Node.js version is installed on your host.
See: https://nodejs.org/en/download/package-manager/
Filenames in brackets are generated by the 'Build' process (via Gulp.js).
project ---- public ---- (ng2) ---- (app) ---- (app.component.js)
| | (app.component.js.map)
| | (app.module.js)
| | (app.module.js.map)
| | (main.js)
| | (main.js.map)
| -- index.html
| styles.css
| systemjs.config.js
|
-- src ---- ng2 ---- app ---- app.component.ts
| | app.module.ts
| | main.ts
| |
| -- server.js
| tsconfig.json
|
-- package.json
gulpfile.js
README.md
.gitignore
The following files are used:
- package.json Node.js configuration (see description)
- gulpfile.js Gulp configuration (see description)
- tsconfig.json: Configuration for TypeScript transpiler tsc
- index.html: Main web page loaded by web-client
- styles.css: Stylesheet used by index.html
- server.js: The Web-Server Node.js application, based on Node.js Express.
... and for Angular 2:
- app.components.ts: Angular 2 component AppComponent
Angular applications are made up of components. A component is the combination of an HTML template and a component class that controls a portion of the screen. This component is used to replace the content of the my-app selector element in index.html
See also https://angular.io/docs/ts/latest/quickstart.html - app.module.ts: Angular 2 module
Angular Modules help organize an application into cohesive blocks of functionality.
See also https://angular.io/docs/ts/latest/guide/ngmodule.html - main.ts: Start of Angular 2 application
This file is used to bootstrap the Angular 2 application.
See also https://angular.io/docs/ts/latest/guide/ngmodule.html#!#bootstrap - systemjs.config.js: systemjs configuration (dynamic module loader)
See package.json and https://www.npmjs.com/~angular
The module prefix @angular
is used for all stable Angular 2 framework modules.
@angular/core
: Angular core framework@angular/common
: Angular commonly needed directives and services@angular/compiler
: Angular compiler library@angular/platform-browser
: Angular library for using Angular in a web browser@angular/platform-browser-dynamic
: Angular library for using Angular in a web browser with JIT compilationrxjs
: Reactive Extensions Library for JavaScript (needed be Angular 2 framework)
see: https://www.npmjs.com/package/rxjszone.js
: library for dynamic extent of asynchronous calls (needed by Angular 2 framework)
see: https://www.npmjs.com/package/zone.jsreflect-metadata
: library to add Decorators
see: https://www.npmjs.com/package/reflect-metadata
Other dependencies:
express
: Web-Server see: https://www.npmjs.com/package/expressmorgan
: HTTP request Logger middleware see: https://www.npmjs.com/package/morgan
Development environment dependencies:
typescript
: TypeScript transpiler see: https://www.npmjs.com/package/typescriptgulp
: Build system Gulp.js see: https://www.npmjs.com/package/gulpgulp-sourcemaps
: Source map support for Gulp.js see: https://www.npmjs.com/package/gulp-sourcemapsgulp-typescript
: TypeScript compiler for Gulp.js with incremental compilation support see: https://www.npmjs.com/package/gulp-typescriptdel
: Delete files and folders (used by Gulp.js task Clean) see: https://www.npmjs.com/package/del
To do (on Linux OS) ...
- open shell and clone git repository
git clone <repository-url>
- change working directory to project and install node modules
cd <project-dir>/dingo
npm install
- transpile the project and start the server
npm start
- start a web-client and test the server
http://localhost:8080
With Gulp.js as build tool a transpilation of the typescript files is needed, before the server can be started.
This transpilation is started automatically with the command npm start
, because
all the needed steps are configured in the file package.json.
...
"scripts": {
"prestart": "./node_modules/gulp/bin/gulp.js",
"start": "node src/server.js"
},
...
See also http://stackoverflow.com/questions/29939697 and https://docs.npmjs.com/misc/scripts for more information.
The following script values are available:
- prepublish, publish, postpublish
- preinstall, install, postinstall
- preuninstall, uninstall, postuninstall
- preversion, version, postversion
- pretest, test, posttest
- prestop, stop, poststop
- prestart, start, poststart
- prerestart, restart, postrestart
- preCUSTOM and postCUSTOM for custom script names.
To install gulp as global module, execute:
sudo npm install -g gulp
In the following subsections you can see how to create and build a Express/Angular 2 project with Netbeans from scratch.
- New Project:
- Categories: HTML5/JavaScript
- Projects: HTML5/JS Application with Node.js
- Next -> Select project name and project location
- Next -> Unselect Enable Express
- Next -> Create package.json
- Finish
Delete the file main.js
Set proper author and repository and add the following entries to prevent warnings and to enable some npm scripts.
{
"scripts": {
"start": "node src/server.js"
},
"description": "Netbeans, Gulp , Express, Angular2, Typescript",
"license": "ISC",
"private": true
}
Execute in shell on project directory location ...
npm install zone.js --save
npm install rxjs --save
npm install reflect-metadata --save
npm install systemjs --save
npm install express --save
npm install morgan --save
npm install @angular/core --save
npm install @angular/common --save
npm install @angular/compiler --save
npm install @angular/platform-browser --save
npm install @angular/platform-browser-dynamic --save
npm install typescript --save-dev
npm install gulp --save-dev
npm install gulp-sourcemaps --save-dev
npm install gulp-typescript --save-dev
npm install del --save-dev
Create the directory src and all files and subdirectories inside using the project file structure shown above.
Create the project files index.html, styles.css and inside the directory public using the project file structure shown above.
Create the file systemjs.config.js inside the directory public.
systemjs is a modern dynamic module loader, which is needed to support javascript statements
like require("@angular/platform-browser-dynamic")
.
See also: https://www.npmjs.com/package/systemjs
See also: https://angular.io/docs/ts/latest/guide/typescript-configuration.html#tsconfig
For transforming the TypeScript source files into ES5 Javascript (which could be executed by each Browser),
the command tsc
from the Node.js module typescript is used.
This tool looks for a configuration file tsconfig.json, before it starts to transform all .ts files
which can be found in the project directory (and the subdirectories).
Create the file tsconfig.json in the project base folder. Instead of setting every typscript source file (option files), the option include is used. See also https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. This makes it more comfortable for the user, because there is no need to change tsconfig.js in case of new, removed or renamed source file.
Create the file gulpfile.js in the project base folder
Open Netbeans Project Properties (right mouse click on project) and set...
-
Category Sources:
- Site Root Folder: public
- Site Source Folder: src
-
Catagory Run:
- Run As: Node.js Application
- Start File: <project-dir>/src/server.js
- Run Browser: selected
- Start file: index.html
- Project URL: http://localhost:8080
Now you can select Run Project. Gulp will build the project and you should see a running Angular 2 application in http://localhost:8080.
Create the file .gitignore and make sure, that the npm modules under node_modules, the files in nbproject/private and the .js in public/app (created by Gulp.js) are not under version control.
The Netbeans 8.2 HTML 5 Webkit and TypeScript plugins does not support some wanted features:
See also: http://netbeans.org/bugzilla/show_bug.cgi?id=257587#c11
-
Debugging of Typescript files inside Netbeans is not possible
Workarounds:
- don't use TypeScript for Node.js server app
- use Chrome web-browser to run TypeScript files (not working with Node.js modules)
-
Netbeans does not support multiple source directories:
Multiple source directories would make sense to seperate server app from client app. Workarounds:- use subdirectory src for client app and server app and locate tsconfig.json in this directory
- make directory inside subdirectory for client app
- set project properties, category Sources, Source Folder to this subdirectory src
Disadvantage:
tsconfig.json, which is build configuration file is now in the src directory, and therefore visible under category Sources. A better place for this file would be the category *Important Files" -
Netbeans does not detect some other "important files"
Files like README.md and tsconfig.ts are only shown in project management browser if they are located in the (configured) source directory. It would make sense to locate them on project root (like the file package.json) and to show them in the category "Important files". This desired behavior is not implemented in the Netbeans 8.2 HTML 5 Kit. Workarounds:- for tsconfig.ts: locate the file in subdirectory src
- for README.md: no workaround known; use category Files in project browser to find this file