forked from bangui/TEACHING-MATERIALS
Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
18 changed files
with
252 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
'use strict' | ||
|
||
const request = require('supertest') | ||
const server = require('../index.js') | ||
const status = require('http-status-codes') | ||
|
||
afterAll( () => server.close()) | ||
|
||
describe('GET /', () => { | ||
test('the page should return a status code of "200 OK"', async done => { | ||
expect.assertions(1) | ||
const response = await request(server).get('/') | ||
expect(response.status).toEqual(status.OK) | ||
done() | ||
}) | ||
test('we should see the text "Hello World" displayed', async done => { | ||
expect.assertions(1) | ||
const response = await request(server).get('/') | ||
expect(response.text).toEqual('Hello World') | ||
done() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
'use strict' | ||
|
||
const request = require('supertest') | ||
const server = require('../index.js') | ||
|
||
afterAll( () => server.close()) | ||
|
||
describe('GET /', () => { | ||
test('we should see the text "Hello World" displayed', async done => { | ||
expect.assertions(1) | ||
const response = await request(server).get('/') | ||
expect(response.text).toEqual('Hello World') | ||
done() | ||
}) | ||
}) | ||
|
||
describe('GET /anon', () => { | ||
test('we should see the text "Hello World" displayed', async done => { | ||
expect.assertions(1) | ||
const response = await request(server).get('/anon') | ||
expect(response.text).toEqual('Hello World') | ||
done() | ||
}) | ||
}) | ||
|
||
describe('GET /books/:index', () => { | ||
test('passing index 0 returns "The Hobbit"', async done => { | ||
expect.assertions(1) | ||
const response = await request(server).get('/books/0') | ||
expect(response.text).toEqual('The Hobbit') | ||
done() | ||
}) | ||
test('passing index 2 returns "The Secret Garden"', async done => { | ||
expect.assertions(1) | ||
const response = await request(server).get('/books/2') | ||
expect(response.text).toEqual('The Secret Garden') | ||
done() | ||
}) | ||
}) | ||
|
||
describe('GET /name', () => { | ||
test('browser should display all querystrings as a JSON string', async done => { | ||
expect.assertions(1) | ||
const response = await request(server).get('/name?firstname=Mark') | ||
expect(response.text).toEqual('{"firstname":"Mark"}') | ||
done() | ||
}) | ||
}) | ||
|
||
describe('GET /hello/:name', () => { | ||
test('browser should display the name in the URL', async done => { | ||
expect.assertions(1) | ||
const response = await request(server).get('/hello/Mark') | ||
expect(response.text).toEqual('hello Mark') | ||
done() | ||
}) | ||
test('the "format" querystring should make name uppercase', async done => { | ||
expect.assertions(1) | ||
const response = await request(server).get('/hello/Mark?format=upper') | ||
expect(response.text).toEqual('hello MARK') | ||
done() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
'use strict' | ||
|
||
const express = require('express') | ||
const app = express() | ||
const port = 8080 | ||
|
||
function hello(req, res) { | ||
res.send('Hello World') | ||
} | ||
|
||
app.get('/', hello) | ||
|
||
app.get('/hello', hello) | ||
|
||
app.get('/anon', (req, res) => { | ||
res.send('Hello World') | ||
}) | ||
|
||
const books = ['The Hobbit', 'Alice in Wonderland', 'The Secret Garden'] | ||
|
||
app.get('/books/:index', (req, res) => { | ||
const parameters = req.params | ||
console.log(parameters) | ||
const title = books[parameters.index] | ||
res.send(title) | ||
}) | ||
|
||
app.get('/name', (req, res) => { | ||
const queryStrings = req.query | ||
console.log(queryStrings) | ||
}) | ||
|
||
app.get('/hello/:name', (req, res) => { | ||
console.log(req.params) | ||
let myname = req.params.name | ||
if(req.query.format === 'upper') myname = myname.toUpperCase() | ||
console.log(`myname: ${myname}`) | ||
res.send(`hello ${myname}`) | ||
}) | ||
|
||
app.listen(port, () => { | ||
console.log(`app listening on port ${port}`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,46 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict' | ||
|
||
const express = require('express') | ||
const app = express() | ||
const Koa = require('koa') | ||
const Router = require('koa-router') | ||
const app = new Koa() | ||
const router = new Router() | ||
|
||
const port = 8080 | ||
|
||
function hello(req, res) { | ||
res.send('Hello World') | ||
function hello(ctx) { | ||
ctx.body = 'Hello World' | ||
} | ||
|
||
app.get('/', hello) | ||
|
||
app.get('/hello', hello) | ||
router.get('/', hello) | ||
|
||
app.get('/anon', (req, res) => { | ||
res.send('Hello World') | ||
router.get('/anon', ctx => { | ||
ctx.body = 'Hello World' | ||
}) | ||
|
||
const books = ['The Hobbit', 'Alice in Wonderland', 'The Secret Garden'] | ||
|
||
app.get('/books/:index', (req, res) => { | ||
const parameters = req.params | ||
router.get('/books/:index', ctx => { | ||
const parameters = ctx.params | ||
console.log(parameters) | ||
const title = books[parameters.index] | ||
res.send(title) | ||
ctx.body = title | ||
}) | ||
|
||
app.get('/name', (req, res) => { | ||
const queryStrings = req.query | ||
router.get('/name', ctx => { | ||
const queryStrings = ctx.query | ||
console.log(queryStrings) | ||
ctx.body = JSON.stringify(queryStrings) | ||
}) | ||
|
||
app.get('/hello/:name', (req, res) => { | ||
console.log(req.params) | ||
let myname = req.params.name | ||
if(req.query.format === 'upper') myname = myname.toUpperCase() | ||
router.get('/hello/:name', ctx => { | ||
console.log(ctx.params) | ||
let myname = ctx.params.name | ||
if(ctx.query.format === 'upper') myname = myname.toUpperCase() | ||
console.log(`myname: ${myname}`) | ||
res.send(`hello ${myname}`) | ||
ctx.body = `hello ${myname}` | ||
}) | ||
|
||
|
||
|
||
app.listen(port, () => { | ||
console.log(`app listening on port ${port}`) | ||
}) | ||
app.use(router.routes()) | ||
module.exports = app.listen(port, () => console.log(`listening on port ${port}`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
'use strict' | ||
|
||
module.exports = { | ||
verbose: true, | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
Oops, something went wrong.