Skip to content

Commit 39c8a6c

Browse files
committed
Update
1. Added some more tests etc 2. Added github workflows 3. Added Dockerfile and support Signed-off-by: James Turnbull <[email protected]>
1 parent ba1ecb9 commit 39c8a6c

File tree

10 files changed

+2119
-1157
lines changed

10 files changed

+2119
-1157
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.github/workflows/docker.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Publish Docker image
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build-and-push:
10+
runs-on: ubuntu-latest
11+
steps:
12+
-
13+
name: Checkout
14+
uses: actions/checkout@v2
15+
-
16+
name: Set up QEMU
17+
uses: docker/setup-qemu-action@v1
18+
-
19+
name: Set up Docker Buildx
20+
uses: docker/setup-buildx-action@v1
21+
-
22+
name: Login to DockerHub
23+
uses: docker/login-action@v1
24+
with:
25+
username: ${{ secrets.DOCKERHUB_USERNAME }}
26+
password: ${{ secrets.DOCKERHUB_TOKEN }}
27+
-
28+
name: Login to GitHub Container Registry
29+
uses: docker/login-action@v1
30+
with:
31+
registry: ghcr.io
32+
username: ${{ github.repository_owner }}
33+
password: ${{ secrets.GHRC_TOKEN }}
34+
-
35+
name: Build and push
36+
uses: docker/build-push-action@v2
37+
with:
38+
context: .
39+
file: ./Dockerfile
40+
push: true
41+
tags: |
42+
jamtur01/animal-farm-nodejs:latest
43+
ghcr.io/jamtur01/animal-farm-nodejs:latest

.github/workflows/test.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Animal Farm Node.js CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
build:
13+
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout repository
18+
uses: actions/checkout@v2
19+
- name: Use Node.js
20+
uses: actions/setup-node@v1
21+
with:
22+
node-version: '14.x'
23+
- name: Run Yarn
24+
run: yarn
25+
- name: Run tests
26+
run: yarn test

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
node_modules
2+
coverage
3+
.nyc_output

Dockerfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM node:14
2+
3+
# Create app directory
4+
WORKDIR /usr/src/app
5+
6+
# Install app dependencies
7+
COPY package.json yarn.lock ./
8+
RUN yarn install
9+
10+
# Bundle app source
11+
COPY . .
12+
13+
EXPOSE 8080
14+
15+
CMD [ "node", "app.js" ]

README.md

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,38 @@
11
# Animal Farm
22

3-
A sample application in Javascript on NodeJS for Github README.
3+
A sample Express application written in Node.js for a Github README post.
4+
5+
## Prerequisites
6+
7+
* Node.js
8+
* yarn
9+
10+
### Install modules
11+
12+
Run `yarn` to install the required node modules.
13+
14+
```shell
15+
yarn install
16+
```
417

518
## Running
619

7-
You can run the sample app in a couple of different ways. The first is to simply launch the executable:
20+
You can run the sample app in a couple of different ways. The first is to launch the application via `yarn`:
21+
22+
```shell
23+
yarn start
824
```
25+
26+
Or you can directly run it via `node`:
27+
28+
```shell
929
node app.js
1030
```
31+
32+
## Tests
33+
34+
You can also run the tests via `yarn`:
35+
36+
```shell
37+
yarn test
38+
```

app.js

+36-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
1-
var express = require('express');
2-
var app = express();
3-
var _ = require('underscore');
4-
var sample = require('lodash.sample');
5-
var animals = [ "aardvark", "bear", "cat", "eel", "frog" ]
6-
var sounds = [ "bark", "meow", "squeak", "roar", "hiss" ]
1+
const express = require('express');
2+
const _ = require('underscore');
3+
4+
var port = process.env.PORT || 8080;
5+
var animals = {
6+
"cat": "meow",
7+
"dog": "bark",
8+
"eel": "hiss",
9+
"bear": "roar",
10+
"frog": "croak"
11+
}
12+
13+
function getAnimal() {
14+
return animal = _.sample(Object.entries(animals));
15+
}
16+
17+
const app = express();
718

819
app.get('/', function(req, res){
9-
res.writeHead(200, { 'Content-Type': 'text/html' });
10-
res.write(`The ${ _.sample(animals) } went ${ _.sample(sounds) }.`);
11-
res.end();
20+
const [animal_name, sound] = getAnimal();
21+
res.writeHead(200, { 'Content-Type': 'text/html' });
22+
res.write(`George Orwell had a farm.<br />
23+
E-I-E-I-O<br />
24+
And on his farm he had a ${ animal_name }.<br />
25+
E-I-E-I-O<br />
26+
With a ${ sound }-${ sound } here.<br />
27+
And a ${ sound }-${ sound } there.<br />
28+
Here a ${ sound }, there a ${ sound }.<br />
29+
Everywhere a ${ sound }-${ sound }.<br />`);
30+
res.end();
1231
});
1332

14-
var port = process.env.PORT || 8080;
15-
console.log(`Launching server on http://localhost:${ port }`)
16-
app.listen(port);
33+
app.get('/api', function(req, res){
34+
res.writeHead(200, { 'Content-Type': 'application/json' });
35+
res.write(JSON.stringify(animals));
36+
res.end();
37+
})
1738

18-
module.exports = app;
39+
module.exports = app.listen(port, () => {
40+
console.log(`Launching server on http://localhost:${ port }`)
41+
});

package.json

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
"name": "animal-farm-nodejs",
33
"license": "MIT",
44
"version": "0.0.1",
5-
"engines": {
6-
"node": "12.x"
7-
},
85
"dependencies": {
96
"express": "4.x",
10-
"lodash.sample": "^4.2.1",
11-
"mocha": "8.2.1",
12-
"supertest": "6.1.3",
137
"underscore": "^1.12.0"
148
},
159
"scripts": {
16-
"test": "./node_modules/mocha/bin/mocha --exit",
10+
"test": "nyc --reporter=html mocha --exit",
1711
"start": "node app.js"
12+
},
13+
"devDependencies": {
14+
"@types/express": "^4.17.11",
15+
"@types/supertest": "^2.0.10",
16+
"@types/underscore": "^1.10.24",
17+
"mocha": "^8.2.1",
18+
"nyc": "^15.1.0",
19+
"supertest": "^6.1.3"
1820
}
1921
}

test/test.js

+30-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
1-
const request = require('supertest')
21
const app = require('../app.js');
2+
const request = require('supertest')(app);
33

44
describe('GET', function(){
55
it('respond with text/html', function(done){
6-
request(app)
7-
.get('/')
8-
.set('Accept', 'text/html')
9-
.expect('Content-Type', /html/)
10-
.expect(200, done);
11-
})
12-
})
6+
request
7+
.get('/')
8+
.set('Accept', 'text/html')
9+
.expect('Content-Type', /html/)
10+
.expect(200, done);
11+
})
12+
13+
it('respond with George Orwell', function(done){
14+
request
15+
.get('/')
16+
.set('Accept', 'text/html')
17+
.expect(200, /George Orwell had a farm/ig, done);
18+
})
19+
20+
it('/api responds with json', function(done){
21+
request
22+
.get('/api')
23+
.set('Accept', 'application/json')
24+
.expect('Content-Type', /json/)
25+
.expect(200, done);
26+
})
27+
28+
it('/api responds with animals object', function(done){
29+
request
30+
.get('/api')
31+
.set('Accept', 'application/json')
32+
.expect(200, {"cat":"meow","dog":"bark","eel":"hiss","bear":"roar","frog":"croak"}, done);
33+
})
34+
})

0 commit comments

Comments
 (0)