Skip to content

Commit 6dbb9d1

Browse files
committed
Make a JSON API for the frontend to talk to
1 parent d231f1a commit 6dbb9d1

File tree

7 files changed

+56
-17
lines changed

7 files changed

+56
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ npm install
2121
- mac: `npm start`
2222
- windows: `nw C:\path\to\node-webkit-angular-tdd-sample`
2323
- linux: ``nw `pwd` ``
24-
- Run the tests: `npm test`
24+
- Run the tests: `npm test && npm run-script acceptance-test`
2525
- Build packaged app: `grunt nodewebkit` (output is in `./build/releases`)
2626

acceptance/HelloTest.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ describe('Hello World', function() {
1515
expect(element(by.css("h1")).getText()).toBe("Hello Arthur!");
1616
});
1717
});
18+
19+
it('shows the node version', function() {
20+
expect(element(by.css("p")).getText()).toMatch(/We are using node\.js v\d+\.\d+\.\d+/);
21+
});
1822
});

main.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
var connect = require('connect');
22
var http = require('http');
3-
43
var web = connect().use(connect.static('public'));
54

5+
var restify = require('restify');
6+
var api = restify.createServer();
7+
api.use(restify.fullResponse());
8+
api.use(function(request, response, next) {
9+
response.once('header', function() {
10+
response.setHeader('Access-Control-Allow-Origin', 'http://localhost:3020');
11+
});
12+
next();
13+
});
14+
api.get('/node', function(request, response, next) {
15+
response.send({ version: process.version });
16+
});
17+
618
http.createServer(web).listen(3020, 'localhost');
19+
api.listen(3021, 'localhost');

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"protractor": "~0.20.1"
4949
},
5050
"dependencies": {
51-
"connect": "~2.14.1"
51+
"connect": "~2.14.1",
52+
"restify": "~2.6.3"
5253
}
5354
}

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<div class="container">
1212
<div class="jumbotron" ng-controller="HelloController">
1313
<h1>Hello<span ng-if="name"> </span>{{ name }}!</h1>
14-
<p>We are using node.js <script>document.write(process.version)</script>.</p>
14+
<p ng-init="fetchNodeInfo()">We are using node.js {{ version }}.</p>
1515

1616
<label for="name">Name</label>
1717
<input class="form-control" id="name" ng-model="name"/>

public/js/HelloController.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
'use strict';
22

3-
angular.module("HelloApp", []).controller('HelloController', ['$scope',
4-
function($scope) {
5-
$scope.name = "World";
6-
}]);
3+
angular.module("HelloApp", []).controller('HelloController', ['$scope', '$http',
4+
function($scope, $http) {
5+
$scope.name = "World";
6+
7+
$scope.fetchNodeInfo = function() {
8+
$http.get("http://localhost:3021/node")
9+
.success(function(data) {
10+
$scope.version = data.version;
11+
});
12+
};
13+
}
14+
]);

test/HelloControllerTest.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
describe('HelloController', function() {
2-
var $scope;
2+
var $scope;
33

4-
beforeEach(module('HelloApp'));
4+
beforeEach(module('HelloApp'));
55

6-
beforeEach(inject(function($rootScope, $controller) {
7-
$scope = $rootScope.$new();
8-
$controller('HelloController', { '$scope': $scope });
9-
}));
6+
beforeEach(inject(function($rootScope, $controller) {
7+
$scope = $rootScope.$new();
8+
$controller('HelloController', {
9+
'$scope': $scope
10+
});
11+
}));
1012

11-
it('sets the initial name', function() {
12-
expect($scope.name).toEqual('World');
13-
});
13+
it('sets the initial name', function() {
14+
expect($scope.name).toEqual('World');
15+
});
16+
17+
describe('fetchNodeInfo()', function() {
18+
it('requests the data from the backend', inject(function($httpBackend) {
19+
$httpBackend.whenGET("http://localhost:3021/node").respond({
20+
version: "99.1"
21+
});
22+
$scope.fetchNodeInfo();
23+
$httpBackend.flush();
24+
expect($scope.version).toBe("99.1");
25+
}));
26+
});
1427
});

0 commit comments

Comments
 (0)