// Call the console.log function.
console.log("Hello World");
// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
const PI = Math.PI;
exports.area = (r) => PI * r * r;
exports.circumference = (r) => 2 * PI * r;
const circle = require('./circle.js');
var area = circle.area(4);
console.log(`Area of circle of radius 4 is ' + area);
index.js
circle.js
{
"name": "module-name",
"version": "1.0.0",
"devDependencies": {
"jasmine": "*"
"lodash": "3.9.3”
},
"dependencies":{}
}
npm install
npm install lodash --save
describe("A suite", function() {
it("has a spec with an expectation", function() {
expect(true).toBe(true);
});
});
describe("A suite", function() {
//it("has a spec with an expectation",
//function() {
// expect(true).toBe(true);
// });
});
//describe("A suite", function() {
it("has a spec with an expectation",
function() {
// expect(true).toBe(true);
});
//});
//describe("A suite", function() {
//it("has a spec with an expectation",
//function() {
//expectation passes
expect(true).toBe(true);
//expectation fails
expect(false).toBe(true);
//});
//});
A modern JavaScript utility library delivering modularity, performance, & extras.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sample HTML Page</title>
</head>
<body>
This is a sample HTML page
</body>
</html>
Static HTML
Rendered Page
components
services
filters etc.
<html>
<body>
<div ng-app="myApp"></div>
</body>
</html
var app = angular.module('myApp', [/*list of app's dependencies*/]);
//app variable is an instance of our module
//the Module api has methods we can use to create services, components etc.
app.service("myService", function(){
//service code here
});
app.component("myComponent", function() {
//component code here
});
//best practice though is to get reference to module as shown below
angular.module("myApp").service("someService", function(){});
app.js
index.html
Spec gives us a way to create custom html elements
//todo-app contents
<create-todo></create-todo>
<todo-list></todo-list>
<todo-filter></todo-filter>
<body ng-app>
<menu></menu>
<user-info></user-info>
<message></message>
</body>
Use the .component() method which takes two arguments
angular.module('module')
.component('myModule', {});
var myComponent = {
templateUrl: "my-template.html",
template: "<div></div>"
controller: MyController,
bindings: {
title: '<',
description : '&'
}
}
function MyController(){
}
angular.module("myModule").component("myComponent", myComponent);
javascript: "my-component.component.js"
html: "my-component.component.html"
var myComponent = {
templateUrl: "my-template.html",
template: "<div></div>"
//controller: MyController,
//bindings: {
// title: '<',
// description : '&'
//}
}
//function MyController(){
//}
angular.module("myModule").component("myComponent", myComponent);
This is the view for the controller
//component.html
<div>
<p>
{{$ctrl.text}}
</p>
</div>
Unless the template is very small, we will want to move template into it's own file and use templateUrl property
//component.js
var myComponent = {
templateUrl: "component.html"
}
var myComponent = {
//templateUrl: "my-template.html",
//template: "<div></div>"
controller: MyController,
//bindings: {
// title: '<',
// description : '&'
//}
}
function MyController(){
}
angular.module("myModule").component("myComponent", myComponent);
Angular then makes the return value from calling "new Controller()" available to the view (our template) via a variable named "$ctrl"
Best practice is to put any initialization code for component in the $onInit function
Components can implement "lifecycle hooks"
These are methods that will be called at certain points in the life of the component
var myComponent = {
//templateUrl: "my-template.html",
//template: "<div></div>"
//controller: MyController,
bindings: {
title: '<',
description : '&'
}
}
//function MyController(){
//}
angular.module("myModule").component("myComponent", myComponent);
Components use '&' binding as outputs
These outputs can be things like callbacks or "Output Events"
var myComponent = {
bindings: {
onUpdate: '&',
onDelete: '&'
}
}
Directives can have the following types:
<my-dir></my-dir>
<span my-dir="exp"></span>
<!-- directive: my-dir exp -->
<span class="my-dir: exp;"></span>
{{expression | filter }}
//filters can take arguments
{{expression | filter : arg1 : arg2 }}
Controllers are instantiated when needed and discarded when they are not. Services give us a means for us to keep data around for lifetime of the app and to share data and logic across components.
Services provide interface for grouping related methods and allow us to share these methods across controllers.
Services are singleton objects and are lazily instantiated, meaning they are only loaded when a piece of our application depends on it.
service()
factory()
constant(key, value)
value(key, value)
var app = angular.module('myApp', []);
app.service('myService', function(){});
app.factory('myService', function(){});
app.constant('myConstant', "a constant string");
app.value('myValue', "some value");
File name follows pattern: "my-service.service.js"
There are also gulp/grunt tasks that will automatically create the $inject property annotation for you
var app = angular.module('myApp');
//inline array annotation
app.service('myService',
['serviceA', function(serviceA){
}]);
//$inject property
function myService (serviceA){
}
myService.$inject = ["serviceA"];
app.service('myService', myService);
//implicit annotation
app.service('myService', function(serviceA){
});
Angular provides $http service which wraps the browser’s XMLHttpRequest object. The service is a function that takes in an object that configures an HTTP request
$http({ method: 'GET', url: '/api/sample/data'});
This method returns a promise
$http.get('/api/sample/data');
$http.post('/api/sample/data', data);
$http.put('/api/sample/data', data);
$http.delete('/api/sample/data', data);
$http.get('/api/sample/data', { cache: true });
Postman
https://slack.com/api/channels.history?token=<token>&channel=<channelId>&count=<numberToGet>
Routing framework for Angular that allows you to set up states within your application
Not packaged with Angular - has to be added as a dependency
//npm install angular-ui-router
angular.module('myApp', ['ui.router']);
$stateProvider
.state('message', {
url: "/message/{id}",
template: '<message></message>'
});
Tells UI-Router where to render the app's current state
<div ui-view="viewA"></div>
Binds a link (<a> tag) to a state. If the state has an associated URL, the directive will automatically generate & update the href attribute
Clicking the link will trigger a state transition
<a ui-sref="home">Home</a>