= 🦴 Bare Bones Angular and Angular CLI Tutorial
:author: Matt Raible 📧 [email protected] :revnumber: 17.0.0 :revdate: {docdate} :subject: Angular and Angular CLI :keywords: Angular, Angular CLI, TypeScript, JavaScript, Node, npm, Jasmine, Cypress :icons: font :lang: en :language: javadocript :sourcedir: . ifndef::env-github[] :icons: font endif::[] ifdef::env-github,env-browser[] :toc: preamble :toclevels: 2 endif::[] ifdef::env-github[] :status: :outfilesuffix: .adoc :!toc-title: :caution-caption: 🔥 :important-caption: ❗ :note-caption: 📎 :tip-caption: 💡 :warning-caption: ⚠️ endif::[] :toc: macro :source-highlighter: highlight.js
IMPORTANT: For a book of this tutorial, please check out https://www.infoq.com/minibooks/angular-mini-book/[The Angular Mini-Book]. Its "Build an Angular App" chapter was inspired by this tutorial.
This tutorial shows you how to build a bare-bones search and edit application using https://angular.io[Angular] and https://github.com/angular/angular-cli[Angular CLI] version 17.
toc::[]
ifdef::env-github[] TIP: It appears you're reading this document on GitHub. If you want a prettier view, install https://chrome.google.com/webstore/detail/asciidoctorjs-live-previe/iaalpfgpbocpdfblpnhhgllgbdbchmia[Asciidoctor.js Live Preview for Chrome], then view the https://raw.githubusercontent.com/mraible/ng-demo/main/README.adoc?toc=left[raw document]. Another option is to use the https://gist.asciidoctor.org/?github-mraible%2Fng-demo%2Fmain%2F%2FREADME.adoc[DocGist view]. endif::[]
.Source Code
If you'd like to get right to it, the https://github.com/mraible/ng-demo[source is on GitHub]. To run the app, use ng serve
. To test it, run ng test
. To run its integration tests, run ng e2e
.
Check out the bonus section at the end of this document for Angular Material, Bootstrap, Auth0, and Electron tutorials.
toc::[]
== What you'll build
You'll build a simple web application with Angular CLI, a tool for Angular development. You'll create an application with search and edit features.
== What you'll need
If you don't have Angular CLI installed, install it:
NOTE: IntelliJ IDEA Ultimate Edition has the best support for TypeScript. If you'd rather not pay for your IDE, checkout https://code.visualstudio.com/[Visual Studio Code].
== Create a new Angular project
Create a new project using the ng new
command:
When prompted for the stylesheet format, choose "CSS
" (the default). Accept the default (No) for SSR (Server-Side Rendering) and SSG (Static Site Generation).
This will create a ng-demo
project and run npm install
in it. It takes about a minute to complete, but will vary based on your internet connection speed.
You can see the version of Angular CLI you're using with the ng version
command.
$ ng version
Angular CLI: 17.0.5 Node: 18.18.2 Package Manager: npm 9.8.1 OS: darwin arm64
Angular: ...
If you run this command from the ng-demo
directory, you'll see even more information.
...
Angular: 17.0.5 ... animations, cli, common, compiler, compiler-cli, core, forms ... platform-browser, platform-browser-dynamic, router
== Run the application
The project is configured with a simple web server for development. To start it, run:
You should see a screen like the one below at http://localhost:4200.
[[default-homepage]] .Default homepage image::src/assets/images/default-homepage.png[Default Homepage, 800, scaledwidth="100%"]
You can make sure your new project's tests pass, run ng test
:
== Add a search feature
To add a search feature, open the project in an IDE or your favorite text editor.
=== The Basics
In a terminal window, cd into the ng-demo
directory and run the following command to create a search component.
Open src/app/search/search.component.html
and replace its default HTML with the following:
Add a query
property to src/app/search/search.component.ts
. While you're there, add a searchResults
property and an empty search()
method.
export class SearchComponent implements OnInit { query: string | undefined; searchResults: any;
constructor() { }
ngOnInit(): void { }
search(): void { }
In src/app/app.routes.ts
, modify the routes
constant to add SearchComponent
as the default:
import { Routes } from '@angular/router'; import { SearchComponent } from './search/search.component';
Run ng serve
again you will see a compilation error.
To solve this, open search.component.ts
. Import FormsModule
and JsonPipe
:
import { Component, OnInit } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { JsonPipe } from '@angular/common';
Now you should be able to see the search form.
[[search-component]] .Search component image::src/assets/images/search-without-css.png[Search component, 800, scaledwidth="100%"]
If yours looks different, it's because I trimmed my app.component.html
to the bare minimum.
If you want to add styling for this component, open src/app/search/search.component.css
and add some CSS. For example:
IMPORTANT: The :host
allows you to target the container of the component. It's the only way to target the host element. You can't reach the host element from inside the component with other selectors because it's not part of the component's own template.
This section has shown you how to generate a new component and add it to a basic Angular application with Angular CLI. The next section shows you how to create and use a JSON file and localStorage
to create a fake API.
=== The Backend
To get search results, create a SearchService
that makes HTTP requests to a JSON file. Start by generating a new service.
Create src/assets/data/people.json
to hold your data.
Modify src/app/shared/search/search.service.ts
and provide HttpClient
as a dependency in its constructor.
In this same file, create a getAll()
method to gather all the people. Also, define the Address
and Person
classes that JSON will be marshalled to.
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs';
@Injectable({ providedIn: 'root' }) export class SearchService {
constructor(private http: HttpClient) { }
getAll(): Observable<Person[]> { return this.http.get<Person[]>('assets/data/people.json'); } }
export class Address { street: string; city: string; state: string; zip: string;
constructor(obj?: any) { this.street = obj?.street || null; this.city = obj?.city || null; this.state = obj?.state || null; this.zip = obj?.zip || null; } }
export class Person { id: number; name: string; phone: string; address: Address;
To make these classes easier to consume by your components, create src/app/shared/index.ts
and add the following:
The reason for creating this file is so you can import multiple classes on a single line rather than having to import each individual class on separate lines.
In search.component.ts
, add imports for these classes.
You can now add a proper type to the searchResults
variable. While you're there, modify the constructor to inject the SearchService
.
export class SearchComponent implements OnInit { query: string | undefined; searchResults: Person[] = [];
Then update the search()
method to call the service's getAll()
method.
At this point, if your app is running, you'll see the following message in your browser's console.
To fix the "No provider
" error from above, update app.config.ts
to import and use provideHttpClient
.
import { provideHttpClient } from '@angular/common/http';
Now clicking the search button should work. To make the results look better, remove the <pre>
tag and replace it with the following in search.component.html
.
@if (searchResults.length) {
Then add some additional CSS to search.component.css
to improve its table layout.
table { margin-top: 10px; border-collapse: collapse; }
th { text-align: left; border-bottom: 2px solid #ddd; padding: 8px; }
Now the search results look better.
[[search-results]] .Search results image::src/assets/images/search-results.png[Search Results, 800, scaledwidth="100%"]
But wait, you still don't have search functionality! To add a search feature, add a search()
method to SearchService
.
import { map, Observable } from 'rxjs'; ...
Then refactor SearchComponent
to call this method with its query
variable.
This won't compile right away.
[ERROR] TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'. [plugin angular-compiler]
Since query
will always be assigned (even if it's empty), change its variable declaration to:
This is called a https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#definite-assignment-assertions[definite assignment assertion]. It's a way to tell TypeScript "I know what I'm doing, the variable will be assigned.
"
Now search results will be filtered by the query value you type in.
This section showed you how to fetch and display search results. The next section builds on this and shows how to edit and save a record.
== Add an edit feature
Modify search.component.html
to wrap the person's name with a link.
Add RouterLink
as an import to search.component.ts
so everything will compile:
import { RouterLink } from '@angular/router';
Run the following command to generate an EditComponent
.
Add a route for this component in app.routes.ts
:
import { EditComponent } from './edit/edit.component';
Update src/app/edit/edit.component.html
to display an editable form. You might notice I've added id
attributes to most elements. This is to make it easier to locate elements when writing integration tests.
@if (person) {
Modify EditComponent
to import model and service classes and to use the SearchService
to get data.
import { Component, OnDestroy, OnInit } from '@angular/core'; import { Person, SearchService } from '../shared'; import { Subscription } from 'rxjs'; import { ActivatedRoute, Router } from '@angular/router'; import { FormsModule } from '@angular/forms';
@Component({ selector: 'app-edit', standalone: true, imports: [FormsModule], templateUrl: './edit.component.html', styleUrl: './edit.component.css' }) export class EditComponent implements OnInit, OnDestroy { person!: Person; sub!: Subscription;
constructor(private route: ActivatedRoute, private router: Router, private service: SearchService) { }
async ngOnInit(): Promise { this.sub = this.route.params.subscribe(params => { const id = +params['id']; // (+) converts string 'id' to a number this.service.get(id).subscribe(person => { if (person) { this.person = person; } else { this.gotoList(); } }); }); }
ngOnDestroy(): void { if (this.sub) { this.sub.unsubscribe(); } }
async cancel() { await this.router.navigate(['/search']); }
async save() { this.service.save(this.person); await this.gotoList(); }
Modify SearchService
to contain functions for finding a person by their id and saving them. While you're in there, modify the search()
method to be aware of updated objects in localStorage
.
search(q: string): Observable<Person[]> { if (!q || q === '*') { q = ''; } else { q = q.toLowerCase(); } return this.getAll().pipe( map((data: Person[]) => data .map((item: Person) => !!localStorage['person' + item.id] ? JSON.parse(localStorage['person' + item.id]) : item) .filter((item: Person) => JSON.stringify(item).toLowerCase().includes(q)) )); }
get(id: number): Observable { return this.getAll().pipe(map((all: Person[]) => { if (localStorage['person' + id]) { return JSON.parse(localStorage['person' + id]); } return all.find((e: Person) => e.id === id); })); }
You can add CSS to src/app/edit/edit.component.css
if you want to make the form look a bit better.
:host { display: block; padding: 0 20px; }
At this point, you should be able to search for a person and update their information.
[[edit-form]] .Edit component image::src/assets/images/edit-form.png[Edit form, 800, scaledwidth="100%"]
The <form> in src/app/edit/edit.component.html
calls a save()
function to update a person's data. You already implemented this above.
The function calls a gotoList()
function that appends the person's name to the URL when sending the user back to the search screen.
Since the SearchComponent
doesn't execute a search automatically when you execute this URL, add the following logic to do so in its ngOnInit()
method.
import { ActivatedRoute, RouterLink } from '@angular/router'; import { Subscription } from 'rxjs'; ...
sub!: Subscription;
constructor(private searchService: SearchService, private route: ActivatedRoute) { }
You'll want to implement OnDestroy
and define the ngOnDestroy
method to clean up this subscription.
import { Component, OnDestroy, OnInit } from '@angular/core';
export class SearchComponent implements OnInit, OnDestroy { ...
After making all these changes, you should be able to search/edit/update a person's information. If it works - nice job!
=== Form Validation
One thing you might notice is you can clear any input element in the form and save it. At the very least, the name
field should be required. Otherwise, there's nothing to click on in the search results.
To make name required, modify edit.component.html
to add a required
attribute to the name <input>
and bind it to Angular's validation with #name="ngModel"
. Add a <div>
next to the field to display an error message when validation fails.
<input [(ngModel)]="person.name" name="name" id="name" placeholder="Name" required #name="ngModel"/>
You'll also need to wrap everything in a <form>
element. Add <form>
after the <h3>
tag and close it before the last </div>
. You'll also need to add an (ngSubmit)
handler to the form, give it the name of editForm
, and change the save button to be a regular submit button that's disabled when the form is invalid.
After making these changes, the name field will be required.
[[edit-form-required]] .Edit form with validation image::src/assets/images/edit-form-validation.png[Edit form with validation, 800, scaledwidth="100%"]
In this screenshot, you might notice the address fields are blank and the save button is enabled. This is explained by the error in your console.
If ngModel is used within a form tag, either the name attribute must be set or the form control must be defined as 'standalone' in ngModelOptions.
To fix this, add a name
attribute to all the address fields. For example:
Now values display in all fields, name
is required, and save is disabled when the form is invalid.
[[edit-form-names]] .Edit form with names and validation image::src/assets/images/edit-form-names.png[Edit form with names and validation, 800, scaledwidth="100%"]
To learn more about forms and validation, see https://angular.io/guide/form-validation[Angular's Validating form input documentation].
== Unit and End-to-End Testing
Now that you've built an application, it's important to test it to ensure it works. The best reason for writing tests is to automate your testing. Without tests, you'll likely be testing manually. This manual testing will take longer and longer as your application grows.
In this section, you'll learn to use http://jasmine.github.io/[Jasmine] for unit testing controllers and https://www.cypress.io/[Cypress] for integration testing.
=== Fix the Tests
If you run ng test
, you'll likely get failures for the components and service you created. These failures will be solved as you complete the section below. The ng test
command will start a process that listens for changes so all you need to do is edit/save files and tests will be automatically run again.
TIP: You can use x
and f
prefixes in front of describe
and it
functions to exclude or only run a particular test.
=== Fix the AppComponent
test
If you changed the app.component.html
template as I did, you'll need to modify app.component.spec.ts
to account for the change in HTML. Change its last test to look for an <h1>
element and the welcome message inside it.
Now this test should pass.
=== Unit test the SearchService
Modify src/app/shared/search/search.service.spec.ts
and set up the test's infrastructure (a.k.a. TestBed
) using HttpClientTestingModule
and HttpTestingController
.
import { TestBed } from '@angular/core/testing'; import { SearchService } from './search.service'; import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
describe('SearchService', () => { let service: SearchService; let httpMock: HttpTestingController;
beforeEach(async () => { await TestBed.configureTestingModule({ imports: [HttpClientTestingModule], providers: [SearchService] });
service = TestBed.inject(SearchService);
httpMock = TestBed.inject(HttpTestingController);
});
Now, you will likely see some errors about the test stubs that Angular CLI created for you. You can ignore these for now.
NullInjectorError: R3InjectorError(DynamicTestModule)[SearchService -> HttpClient -> HttpClient]: NullInjectorError: No provider for HttpClient!
HttpTestingController
allows you to mock requests and use its flush()
method to provide response values. Since the HTTP request methods return an Observable
, you can subscribe to it and create expectations in the callback methods. Add the first test of getAll()
to search.service.spec.ts
.
The test below should be on the same level as beforeEach
.
it('should retrieve all search results', () => { const mockResponse = [ {name: 'Nikola Jokić'}, {name: 'Mike Malone'} ];
service.getAll().subscribe((people: any) => { expect(people.length).toBe(2); expect(people[0].name).toBe('Nikola Jokić'); expect(people).toEqual(mockResponse); });
While you're there, add an afterEach()
to verify requests.
Add a couple more tests for filtering by search term and fetching by id.
it('should filter by search term', () => { const mockResponse = [{name: 'Nikola Jokić'}];
service.search('nik').subscribe((people: any) => { expect(people.length).toBe(1); expect(people[0].name).toBe('Nikola Jokić'); });
const req = httpMock.expectOne('assets/data/people.json'); expect(req.request.method).toBe('GET'); req.flush(mockResponse); });
it('should fetch by id', () => { const mockResponse = [ {id: 1, name: 'Nikola Jokić'}, {id: 2, name: 'Mike Malone'} ];
service.get(2).subscribe((person: any) => { expect(person.name).toBe('Mike Malone'); });
=== Unit test the SearchComponent
To unit test the SearchComponent
, you can mock the methods in SearchService
with http://angular-tips.com/blog/2021/07/unit-testing-spies-and-mocks/[spies]. These allow you to spy on functions to check if they were called.
Create src/app/shared/search/mocks/routes.ts
to mock Angular's Router
and ActivatedRoute
.
import { ActivatedRoute } from '@angular/router'; import { of } from 'rxjs';
export class MockActivatedRoute extends ActivatedRoute {
constructor(parameters?: { [key: string]: any; }) { super(); // @ts-ignore this.params = of(parameters); } }
With this mock in place, you can TestBed.configureTestingModule()
to set up SearchComponent
to use it as a provider. In the second beforeEach()
, you can see that the search()
method is spied on and its results are mocked. The response isn't important in this case because you're just unit testing the SearchComponent
.
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { SearchComponent } from './search.component'; import { SearchService } from '../shared'; import { ActivatedRoute } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; import { FormsModule } from '@angular/forms'; import { MockActivatedRoute } from '../shared/search/mocks/routes'; import { of } from 'rxjs'; import { HttpClientTestingModule } from '@angular/common/http/testing';
describe('SearchComponent', () => { let component: SearchComponent; let fixture: ComponentFixture; let mockSearchService: SearchService; let mockActivatedRoute: MockActivatedRoute;
beforeEach(async () => { mockActivatedRoute = new MockActivatedRoute({term: 'nikola'});
await TestBed.configureTestingModule({
declarations: [],
providers: [
{provide: ActivatedRoute, useValue: mockActivatedRoute}
],
imports: [FormsModule, RouterTestingModule, HttpClientTestingModule]
}).compileComponents();
});
beforeEach(() => { // mock response mockSearchService = TestBed.inject(SearchService); mockSearchService.search = jasmine.createSpy().and.returnValue(of([]));
// initialize component
fixture = TestBed.createComponent(SearchComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
Add two tests, one to verify a search term is used when it's set on the component, and a second to verify search is called when a term is passed in as a route parameter.
it('should search when a term is set and search() is called', () => { component = fixture.componentInstance; component.query = 'J'; component.search(); expect(mockSearchService.search).toHaveBeenCalledWith('J'); });
Update the test for EditComponent
, verifying fetching a single record works. Notice how you can access the component directly with fixture.componentInstance
, or its rendered version with fixture.nativeElement
.
import { EditComponent } from './edit.component'; import { TestBed } from '@angular/core/testing'; import { Address, Person, SearchService } from '../shared'; import { MockActivatedRoute, MockRouter } from '../shared/search/mocks/routes'; import { ActivatedRoute, Router } from '@angular/router'; import { FormsModule } from '@angular/forms'; import { of } from 'rxjs'; import { HttpClientTestingModule } from '@angular/common/http/testing';
describe('EditComponent', () => { let mockSearchService: SearchService; let mockActivatedRoute: MockActivatedRoute; let mockRouter: MockRouter;
beforeEach(async () => { mockActivatedRoute = new MockActivatedRoute({id: 1}); mockRouter = new MockRouter();
await TestBed.configureTestingModule({
declarations: [],
providers: [
{provide: ActivatedRoute, useValue: mockActivatedRoute},
{provide: Router, useValue: mockRouter}
],
imports: [FormsModule, HttpClientTestingModule]
}).compileComponents();
mockSearchService = TestBed.inject(SearchService);
});
it('should fetch a single record', () => { const fixture = TestBed.createComponent(EditComponent);
const person = new Person({id: 1, name: 'Michael Porter Jr.'});
person.address = new Address({city: 'Denver'});
// mock response
spyOn(mockSearchService, 'get').and.returnValue(of(person));
// initialize component
fixture.detectChanges();
// verify service was called
expect(mockSearchService.get).toHaveBeenCalledWith(1);
// verify data was set on component when initialized
const editComponent = fixture.componentInstance;
expect(editComponent.person.address.city).toBe('Denver');
// verify HTML renders as expected
const compiled = fixture.nativeElement;
expect(compiled.querySelector('h3').innerHTML).toBe('Michael Porter Jr.');
You should see "Executed 11 of 11 [green]#SUCCESS#" in the shell window that's running
ng test`. If you don't, try canceling the command and restarting.
=== Integration test the search UI
To test if the application works end-to-end, you can write tests with http://www.cypress.io/[Cypress]. These are also known as integration tests since they test the integration between all layers of your application.
You can use the official https://www.npmjs.com/package/@cypress/schematic[Cypress Angular Schematic] to add Cypress to your Angular project.
When prompted to proceed and use Cypress for ng e2e
and component testing, answer "Yes
".
This will add Cypress as a dependency and create configuration files to work with Angular and TypeScript. Rename cypress/e2e/spec.cy.ts
to home.cy.ts
and change it to look for the title of your app.
Then, run ng e2e
. This will compile your app, start it on http://localhost:4200
, and launch the Cypress Electron app. When prompted to choose a browser, select Electron.
.Cypress Electron App image::ng-demo/cypress-electron-app.png[Cypress Electron App, 800, scaledwidth="100%", align=center]
If you click on the file name, it'll launch a browser and run the test. You can use this feature to step through your tests, find selectors for elements, and much more. You can learn more about Cypress' features at https://armno.in.th/2020/02/26/cypress-angular-integration-testing/[Setting up Cypress for an Angular Project].
Personally, I prefer the Protractor experience where you could just run the command, it'd run all the tests, and the user doesn't need to interact. You can do this with Cypress too!
The Cypress Angular Schematic added a few scripts to your package.json
:
To use the no-interaction approach, you'll need to start your app:
Then, run the Cypress tests for it in another window:
You might notice Cypress creates a video. You can disable this by adding video: false
to your cypress.config.ts
file.
====
The npm run cypress:run
command will run a headless browser, so you won't see anything happening on your screen.
If you want to see the tests run, append -- --browser chrome --headed
to the command. Add this to your package.json
if you want to make it the default. See Cypress' https://docs.cypress.io/guides/guides/launching-browsers[launching browsers] documentation to see a list of supported browsers.
You can also install https://www.npmjs.com/package/concurrently[concurrently] so you can run multiple tasks with one command.
Then, add a cy:run
script to your package.json
:
Then, you can run npm run cy:run
to start your app and continuously run end-to-end tests on it when you change files.
=== Testing the search feature
Create another end-to-end test in cypress/e2e/search.cy.ts
to verify the search feature works. Populate it with the following code:
describe('Search', () => {
beforeEach(() => { cy.visit('/search') });
it('should have an input and search button', () => { cy.get('app-root app-search form input').should('exist'); cy.get('app-root app-search form button').should('exist'); });
=== Testing the edit feature
Create a cypress/e2e/edit.cy.ts
test to verify the EditComponent
renders a person's information and that their information can be updated.
describe('Edit', () => {
beforeEach(() => { cy.visit('/edit/1') });
it('should allow viewing a person', () => { cy.get('h3').should('have.text', 'Nikola Jokić'); cy.get('#name').should('have.value', 'Nikola Jokić'); cy.get('#street').should('have.value', '2000 16th Street'); cy.get('#city').should('have.value', 'Denver'); });
With your app running, execute npm run cypress:run
to verify all your end-to-end tests pass. You should see a success message similar to the one below in your terminal window.
.Cypress success image::src/assets/images/cypress-success.png[Cypress success, 800, scaledwidth="100%", align=center]
If you made it this far and have all your specs passing - congratulations! You're well on your way to writing quality code with Angular and verifying it works.
You can see the test coverage of your project by running ng test --no-watch --code-coverage
.
You'll see a print out of code coverage in your terminal window.
You can also open coverage/ng-demo/index.html
in your browser.
You might notice that the EditComponent
could use some additional coverage. If you feel the need to improve this coverage, please create a pull request!
[[test-coverage]] .Test coverage image::src/assets/images/test-coverage.png[Test coverage, 800, scaledwidth="100%"]
== Continuous Integration
At the time of this writing, Angular CLI did not have any continuous integration support. This section shows you how to set up continuous integration with https://github.com/features/actions[GitHub Actions] and https://jenkins.io/[Jenkins].
=== GitHub Actions
If you've checked your project into GitHub, you can use GitHub Actions.
Create a .github/workflows/main.yml
file. Add the following YAML to it. This will run both unit tests and integration tests with Cypress.
name: Angular
on: [push, pull_request]
Check it in on a branch, create a pull request for that branch, and you should see your tests running.
=== Jenkins
If you've checked your project into source control, you can use Jenkins to automate testing.
Jenkinsfile
in the root directory and commit/push it.node { def nodeHome = tool name: 'node-18', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation' env.PATH = "${nodeHome}/bin:${env.PATH}"
stage('check tools') {
sh "node -v"
sh "npm -v"
}
stage('checkout') {
checkout scm
}
stage('npm install') {
sh "npm install"
}
stage('unit tests') {
sh "npm test -- --watch=false"
}
stage('cypress tests') {
sh "npm start &"
sh "npm run cypress:run"
}
[start=2]
. Install https://jenkins.io/download/[Jenkins] on your hard drive and start it.
. Login to Jenkins at http://localhost:8080
and install the Node.js plugin.
. Go to Manage Jenkins > Global Tool Configuration > NodeJS. Install and configure the name of your Node.js installation to match your build script.
. Create a new project with Dashboard > New Item > Pipeline > Pipeline script from SCM (near the bottom). Point it at your project's repository and specify the main
branch.
. Click Save, then Build Now on the following screen.
== Deployment to Heroku
This section shows you how to deploy an Angular app to https://heroku.com[Heroku].
https://signup.heroku.com/[Create a Heroku account], https://devcenter.heroku.com/articles/heroku-cli[install the heroku CLI], and run heroku login
.
Run heroku create
to create an app on Heroku.
Create a config/nginx.conf.erb
file with the configuration for secure headers and redirect all HTTP requests to HTTPS.
daemon off;
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;
events { use epoll; accept_mutex on; worker_connections <%= ENV['NGINX_WORKER_CONNECTIONS'] || 1024 %>; }
http { gzip on; gzip_comp_level 2; gzip_min_length 512; gzip_proxied any; # Heroku router sends Via header
server_tokens off;
log_format l2met 'measure#nginx.service=$request_time request_id=$http_x_request_id';
access_log <%= ENV['NGINX_ACCESS_LOG_PATH'] || 'logs/nginx/access.log' %> l2met;
error_log <%= ENV['NGINX_ERROR_LOG_PATH'] || 'logs/nginx/error.log' %>;
include mime.types;
default_type application/octet-stream;
sendfile on;
# Must read the body in 5 seconds.
client_body_timeout <%= ENV['NGINX_CLIENT_BODY_TIMEOUT'] || 5 %>;
server {
listen <%= ENV["PORT"] %>;
server_name _;
keepalive_timeout 5;
client_max_body_size <%= ENV['NGINX_CLIENT_MAX_BODY_SIZE'] || 1 %>M;
root dist/ng-demo;
index index.html;
location / {
try_files $uri /index.html;
}
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; frame-ancestors 'none'; connect-src 'self' https://*.auth0.com https://*.herokuapp.com";
add_header Referrer-Policy "no-referrer, strict-origin-when-cross-origin";
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options DENY;
add_header X-XSS-Protection "1; mode=block";
add_header Permissions-Policy "geolocation=(self), microphone=(), accelerometer=(), camera=()";
}
NOTE: In this code, you might notice that some https URLs are allowed in the content security policy. Those are there so this app can make XHR requests to those domains when that functionality is added.
For config/nginx.conf.erb
to be read, you have to use the https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-nginx[Heroku NGINX buildpack].
Add a Procfile
to the root of your project.
Commit your changes to Git, add the Node.js + NGINX buildpack, and redeploy your Angular app using git push
.
View the application in your browser with heroku open
. Try your app's URL on https://securityheaders.com to be pleasantly surprised.
TIP: You can watch your app's logs using heroku logs --tail
.
== Source code
A completed project with this code in it is available on GitHub at https://github.com/mraible/ng-demo.
== Summary
I hope you've enjoyed this in-depth tutorial on how to get started with Angular and Angular CLI. Angular CLI takes much of the pain out of setting up an Angular project and using Typescript. I expect great things from Angular CLI, mostly because the Angular setup process can be tedious and CLI greatly simplifies things.
== Bonus: Angular Material, Bootstrap, Auth0, and Electron
If you'd like to see how to integrate https://material.angular.io/[Angular Material], https://getbootstrap.com[Bootstrap], https://developer.auth0.com[Auth0], or https://www.electronjs.org/[Electron] this section is for you!
I've created branches to show how to integrate each of these libraries. Click on the links below to see each branch's documentation.