diff --git a/.eslintrc.json b/.eslintrc.json index 53a7cca..108c6ee 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -41,7 +41,7 @@ "no-self-assign": 2, "no-trailing-spaces": 1, "no-undef": 2, - "no-unused-vars": 1, + "no-unused-vars": "warn", "no-var": 2, "prefer-arrow-callback": 1, "prefer-const": 2, diff --git a/exercises/01_http/01_url/express.js b/exercises/01_http/01_url/express.js deleted file mode 100644 index 07336d2..0000000 --- a/exercises/01_http/01_url/express.js +++ /dev/null @@ -1,44 +0,0 @@ - -'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}`) -}) diff --git a/exercises/01_http/01_url/index.js b/exercises/01_http/01_url/index.js index 63f9af4..e4a9da0 100644 --- a/exercises/01_http/01_url/index.js +++ b/exercises/01_http/01_url/index.js @@ -1,7 +1,5 @@ #!/usr/bin/env node -'use strict' - const Koa = require('koa') const Router = require('koa-router') const app = new Koa() diff --git a/exercises/01_http/02_headers/express.js b/exercises/01_http/02_headers/express.js deleted file mode 100644 index bea055d..0000000 --- a/exercises/01_http/02_headers/express.js +++ /dev/null @@ -1,140 +0,0 @@ - -'use strict' - -const fs = require('fs') -const js2xmlparser = require('js2xmlparser') -const express = require('express') -const bodyParser = require('body-parser') -const app = express() -app.use(express.static('public')) -app.use(bodyParser.urlencoded({ extended: true })) - -const port = 8080 -const minLength = 3 - -const status = { - ok: 200, - created: 201, - notFound: 404, - notAcceptable: 406, - conflict: 409 -} - -const names = [] - -app.get('/', (req, res) => { - res.send('Hello World') -}) - -app.get('/hello', (req, res) => { - res.sendFile(`${__dirname}/hello.html`) -}) - -app.get('/hello/:name', (req, res) => { - console.log(req.params) - const myname = req.params.first - res.send(`hello ${myname}`) -}) - -app.get('/hello/:first/:last', (req, res) => { - console.log(req.params) - const name = { - firstname: req.params.first, - lastname: req.params.last - } - console.log(name) - res.send(`hello ${name.firstname} ${name.lastname}`) -}) - -app.get('/animal/:name', (req, res) => { - console.log(req.params.name) - fs.exists(`public/${req.params.name}.png`, exists => { - if (exists) { - console.log(`found ${req.params.name}.png`) - res.setHeader('content-type', 'image/png') - console.log('sending file') - const filename = `${__dirname}/public/${req.params.name}.png` - console.log(filename) - res.sendFile(filename) - } else { - console.log('not found!') - res.status(status.notFound).send('Animal Not found') - } - }) -}) - -app.get('/form', (req, res) => { - res.sendFile(`${__dirname}/form.html`) -}) - -app.post('/form', (req, res) => { - console.log('processing the file') - if(req.body.firstname.length >= minLength && req.body.lastname.length >= minLength) { - names.push( { firstname: req.body.firstname, lastname: req.body.lastname } ) - res.status(status.created).send(`your name is ${req.body.firstname} ${req.body.lastname}`) - } -}) - -app.get('/names', (req, res) => { - let list = names - let search = 'x' - - if(req.query.search && req.query.search.length >= minLength) { - console.log(`found query parameter: ${req.query.search}`) - search = req.query.search.toLowerCase() - } else if(req.headers.search && req.headers.search.length >= minLength) { - console.log(`found header: ${req.headers.search}`) - search = req.headers.search.toLowerCase() - } - - console.log(req.headers.accepts) - - if(search.length >= minLength) { - console.log(`you are searching for '${search}'`) - console.log(search) - list = names.filter( val => `${val.firstname} ${val.lastname}`.toLowerCase().includes(search)) - } - if(list.length === 0) { - res.status(status.notFound).send('No Names found') - return - } - console.log() - res.format({ - 'text/csv': () => { - res.setHeader('content-type', 'text/csv') - for(const name of list) { - res.write(`${name.firstname}, ${name.lastname}`) - } - res.end() - }, - 'text/html': () => { - console.log('text/html requested') - res.setHeader('content-type', 'text/html') - console.log(names) - res.write('') - for(const name of list) { - console.log(name) - res.write(``) - } - res.write('
${name.firstname}${name.lastname}
') - res.end() - }, - 'application/json': () => { - console.log('application/json requested') - res.setHeader('content-type', 'application/json') - res.status(status.ok).send(list) - }, - 'application/xml': () => { - console.log('application/xml requested') - res.setHeader('content-type', 'application/xml') - res.status(status.ok).send(js2xmlparser.parse('people', list)) - }, - default: () => { - res.status(status.notAcceptable).send('unsupported MIME type') - } - }) -}) - -app.listen(port, () => { - console.log(`app listening on port ${port}`) -}) diff --git a/exercises/01_http/02_headers/index.js b/exercises/01_http/02_headers/index.js new file mode 100644 index 0000000..3572559 --- /dev/null +++ b/exercises/01_http/02_headers/index.js @@ -0,0 +1,172 @@ +#!/usr/bin/env node + +const fs = require('fs-extra') +const js2xmlparser = require('js2xmlparser') +const Koa = require('koa') +const Router = require('koa-router') +const status = require('http-status-codes') +const views = require('koa-views') +const send = require('koa-send') + +const app = new Koa() +const router = new Router() +//const sendfile = require('koa-sendfile') + +app.use(views(`${__dirname}/views`, { extension: 'html' }, {map: { handlebars: 'handlebars' }})) + +const port = 8080 + +const bodyParser = require('koa-bodyparser') +app.use(bodyParser()) + +const staticFiles = require('koa-static') +app.use(staticFiles('./public')) + +const names = [] + +router.get('/', ctx => { + ctx.body = 'Hello World' +}) + +router.get('/hello', async ctx => { + await ctx.render('hello') +}) + +router.get('/hello/:name', ctx => { + console.log(ctx.params) + const myname = ctx.params.name + ctx.body = `hello ${myname}` +}) + +router.get('/hello/:first/:last', ctx => { + console.log(ctx.params) + const name = { + firstname: ctx.params.first, + lastname: ctx.params.last + } + console.log(name) + ctx.body = `hello ${name.firstname} ${name.lastname}` +}) + +router.get('/animal/:name', async ctx => { + try { + console.log(ctx.params.name) + const exists = await fs.pathExists(`public/${ctx.params.name}.png`) + if(exists === false) throw new Error(`There is no ${ctx.params.name} image`) + console.log(`found ${ctx.params.name}.png`) + ctx.set('content-type', 'image/png') + console.log('sending file') + const filename = `./public/${ctx.params.name}.png` + console.log(filename) + await send(ctx, filename) + } catch(err) { + console.log(err.message) + ctx.status = status.NOT_FOUND + ctx.body = err.message + } +}) + +router.get('/form', async ctx => { + await ctx.render('form') +}) + +router.post('/form', async ctx => { + try { + console.log('processing the file') + const min = 3 + const body = ctx.request.body + console.log(body) + if(body.firstname === undefined || body.lastname === undefined) { + throw new Error('missing data in request body') + } + if(body.firstname.length < min || body.lastname.length < min) { + throw new Error('invalid data in request body') + } + names.push( { firstname: body.firstname, lastname: body.lastname } ) + ctx.status = status.CREATED + ctx.body = `your name is ${body.firstname} ${body.lastname}` + } catch(err) { + ctx.status = status.UNPROCESSABLE_ENTITY + ctx.body = err.message + } +}) + +router.get('/names', async ctx => { + try { + const min = 3 + let list = names + let search = 'x' + if(ctx.get('search') && ctx.get('search').length >= min) search = ctx.get('search').toLowerCase() + if(ctx.query.search && ctx.query.search.length >= min) search = ctx.query.search.toLowerCase() + console.log(`search: ${search}`) + if(search.length >= min) { + console.log(`you are searching for '${search}'`) + list = names.filter( val => `${val.firstname} ${val.lastname}`.toLowerCase().includes(search)) + } + if(list.length === 0) { + ctx.status = status.NOT_FOUND + ctx.body = 'No Names found' + return + } + // we have some data to display! + console.log(`accept header: ${ctx.get('accept')}`) + console.log(`type: ${ctx.request.type}`) + let body = '' + let statusCode = status.OK + switch(ctx.get('accept')) { + case 'text/csv': + console.log('plain text') + body = getCsv(list) + break + case 'text/html': + console.log('web page') + body = getHtml(list) + break + case 'application/json': + console.log('JSON data') + body = getJson(list) + break + case 'application/xml': + console.log('xml data') + body = getXml(list) + break + default: + console.log('invalid content type') + statusCode = status.NOT_ACCEPTABLE + } + ctx.set('content-type', ctx.get('accept')) + ctx.status = statusCode + ctx.body = body + + } catch(err) { + // + } +}) + +function getCsv(names) { + let data = '' + for(const name of names) { + data += `${name.firstname}, ${name.lastname}\n` + } + return data +} + +function getHtml(names) { + let data = '' + for(const name of names) { + data += `` + } + data += '
${name.firstname}${name.lastname}
' + return data +} + +function getJson(names) { + return names +} + +function getXml(names) { + return js2xmlparser.parse('people', names) +} + +app.use(router.routes()) +module.exports = app.listen(port, () => console.log(`listening on port ${port}`)) diff --git a/exercises/01_http/02_headers/package.json b/exercises/01_http/02_headers/package.json new file mode 100644 index 0000000..3bbf022 --- /dev/null +++ b/exercises/01_http/02_headers/package.json @@ -0,0 +1,23 @@ +{ + "name": "02_headers", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "fs-extra": "^7.0.1", + "http-status-codes": "^1.3.0", + "js2xmlparser": "^3.0.0", + "koa": "^2.6.2", + "koa-bodyparser": "^4.2.1", + "koa-router": "^7.4.0", + "koa-send": "^5.0.0", + "koa-sendfile": "^2.0.1", + "koa-static": "^5.0.0", + "koa-views": "^6.1.5" + } +} diff --git a/exercises/01_http/02_headers/form.html b/exercises/01_http/02_headers/views/form.html similarity index 100% rename from exercises/01_http/02_headers/form.html rename to exercises/01_http/02_headers/views/form.html diff --git a/exercises/01_http/02_headers/views/hello.html b/exercises/01_http/02_headers/views/hello.html new file mode 100644 index 0000000..e36a0cd --- /dev/null +++ b/exercises/01_http/02_headers/views/hello.html @@ -0,0 +1,10 @@ + + + + + Hello World + + +

Hello World

+ + diff --git a/exercises/01_http/index.js b/exercises/01_http/index.js index d2193bb..f67e740 100644 --- a/exercises/01_http/index.js +++ b/exercises/01_http/index.js @@ -1,6 +1,5 @@ #!/usr/bin/env node -'use strict' const Koa = require('koa') const app = new Koa() diff --git a/exercises/02_html/01_syntax/index.js b/exercises/02_html/01_syntax/index.js index a811083..bf94004 100644 --- a/exercises/02_html/01_syntax/index.js +++ b/exercises/02_html/01_syntax/index.js @@ -1,15 +1,15 @@ +#!/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 views = require('koa-views') const port = 8080 -app.get('/', (req, res) => { - res.sendFile(`${__dirname}/coventry.html`) -}) +app.use(views(`${__dirname}`, { extension: 'html' }, {map: { handlebars: 'handlebars' }})) + +router.get('/', async ctx => await ctx.render('coventry')) -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}`)) diff --git a/exercises/02_html/01_syntax/package.json b/exercises/02_html/01_syntax/package.json new file mode 100644 index 0000000..701e135 --- /dev/null +++ b/exercises/02_html/01_syntax/package.json @@ -0,0 +1,16 @@ +{ + "name": "01_syntax", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "koa": "^2.6.2", + "koa-router": "^7.4.0", + "koa-views": "^6.1.5" + } +} diff --git a/exercises/02_html/02_lists/commodore64.html b/exercises/02_html/02_lists/commodore64.html new file mode 100644 index 0000000..06fdfb2 --- /dev/null +++ b/exercises/02_html/02_lists/commodore64.html @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/exercises/02_html/02_lists/html/computers80.html b/exercises/02_html/02_lists/computers80.html similarity index 94% rename from exercises/02_html/02_lists/html/computers80.html rename to exercises/02_html/02_lists/computers80.html index 091803b..db0b7b4 100644 --- a/exercises/02_html/02_lists/html/computers80.html +++ b/exercises/02_html/02_lists/computers80.html @@ -8,7 +8,7 @@
- A computer with a monitor + A computer with a monitor
Photo: Piotr Siedlecki, public domain via http://www.publicdomainpictures.net.
diff --git a/exercises/02_html/02_lists/express.js b/exercises/02_html/02_lists/express.js new file mode 100644 index 0000000..2e2cb2d --- /dev/null +++ b/exercises/02_html/02_lists/express.js @@ -0,0 +1,17 @@ + +const express = require('express') +const app = express() + +const port = 8080 + +app.get('/', (req, res) => { + res.sendFile(`${__dirname}/html/computers80.html`) +}) + +app.get('/commodore', (req, res) => { + res.sendFile(`${__dirname}/commodore64.html`) +}) + +app.listen(port, () => { + console.log(`app listening on port ${port}`) +}) diff --git a/exercises/02_html/02_lists/index.js b/exercises/02_html/02_lists/index.js index e095b10..f3848e4 100644 --- a/exercises/02_html/02_lists/index.js +++ b/exercises/02_html/02_lists/index.js @@ -1,19 +1,17 @@ +#!/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 views = require('koa-views') +app.use(require('koa-static')('public')) const port = 8080 -app.get('/', (req, res) => { - res.sendFile(`${__dirname}/html/computers80.html`) -}) +app.use(views(`${__dirname}`, { extension: 'html' }, {map: { handlebars: 'handlebars' }})) -app.get('/commodore', (req, res) => { - res.sendFile(`${__dirname}/commodore64.html`) -}) +router.get('/', async ctx => await ctx.render('computers80')) +router.get('/commodore', async ctx => ctx.render('commodore64')) -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}`)) diff --git a/exercises/02_html/02_lists/package.json b/exercises/02_html/02_lists/package.json new file mode 100644 index 0000000..c0e25a7 --- /dev/null +++ b/exercises/02_html/02_lists/package.json @@ -0,0 +1,17 @@ +{ + "name": "02_lists", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "koa": "^2.6.2", + "koa-router": "^7.4.0", + "koa-static": "^5.0.0", + "koa-views": "^6.1.5" + } +} diff --git a/exercises/02_html/02_lists/public/computer.png b/exercises/02_html/02_lists/public/computer.png new file mode 100644 index 0000000..b71ac38 Binary files /dev/null and b/exercises/02_html/02_lists/public/computer.png differ diff --git a/exercises/02_html/03_hypermedia/index.js b/exercises/02_html/03_hypermedia/index.js index 9062f93..f04dec4 100644 --- a/exercises/02_html/03_hypermedia/index.js +++ b/exercises/02_html/03_hypermedia/index.js @@ -1,32 +1,27 @@ - -'use strict' - -const express = require('express') -const app = express() -app.use(express.static('public')) - +#!/usr/bin/env node + +const Koa = require('koa') +const Router = require('koa-router') +const app = new Koa() +const router = new Router() +const views = require('koa-views') +app.use(require('koa-static')('public')) const port = 8080 -app.get('/', (req, res) => { - res.sendFile(`${__dirname}/html/index.html`) -}) - -app.get('/paradoxes', (req, res) => { - res.sendFile(`${__dirname}/html/paradoxes.html`) -}) +app.use(views(`${__dirname}/views`, { extension: 'html' }, {map: { handlebars: 'handlebars' }})) -app.get('/commodore', (req, res) => { - res.sendFile(`${__dirname}/html/commodore64.html`) -}) +router.get('/', async ctx => await ctx.render('index')) +router.get('/commodore', async ctx => ctx.render('commodore64')) +router.get('/paradoxes', async ctx => ctx.render('paradoxes')) +router.get('/cathedral', async ctx => ctx.render('cathedral')) -app.get('/date', (req, res) => { +router.get('/date', async ctx => { const today = new Date() const dd = today.getDate() const mm = today.getMonth()+1 const yyyy = today.getFullYear() - res.send(`

The date is: ${dd}/${mm}/${yyyy}`) + ctx.body = `

The date is: ${dd}/${mm}/${yyyy}` }) -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}`)) diff --git a/exercises/02_html/03_hypermedia/package.json b/exercises/02_html/03_hypermedia/package.json new file mode 100644 index 0000000..b86d93c --- /dev/null +++ b/exercises/02_html/03_hypermedia/package.json @@ -0,0 +1,17 @@ +{ + "name": "03_hypermedia", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "koa": "^2.6.2", + "koa-router": "^7.4.0", + "koa-static": "^5.0.0", + "koa-views": "^6.1.5" + } +} diff --git a/exercises/02_html/03_hypermedia/html/cathedral.html b/exercises/02_html/03_hypermedia/views/cathedral.html similarity index 100% rename from exercises/02_html/03_hypermedia/html/cathedral.html rename to exercises/02_html/03_hypermedia/views/cathedral.html diff --git a/exercises/02_html/03_hypermedia/html/commodore64.html b/exercises/02_html/03_hypermedia/views/commodore64.html similarity index 100% rename from exercises/02_html/03_hypermedia/html/commodore64.html rename to exercises/02_html/03_hypermedia/views/commodore64.html diff --git a/exercises/02_html/03_hypermedia/html/index.html b/exercises/02_html/03_hypermedia/views/index.html similarity index 100% rename from exercises/02_html/03_hypermedia/html/index.html rename to exercises/02_html/03_hypermedia/views/index.html diff --git a/exercises/02_html/03_hypermedia/html/paradoxes.html b/exercises/02_html/03_hypermedia/views/paradoxes.html similarity index 100% rename from exercises/02_html/03_hypermedia/html/paradoxes.html rename to exercises/02_html/03_hypermedia/views/paradoxes.html diff --git a/exercises/02_html/04_tables/index.js b/exercises/02_html/04_tables/index.js index f6c7a0d..f78d6a4 100644 --- a/exercises/02_html/04_tables/index.js +++ b/exercises/02_html/04_tables/index.js @@ -1,16 +1,16 @@ +#!/usr/bin/env node -'use strict' - -const express = require('express') -const app = express() -app.use(express.static('public')) - +const Koa = require('koa') +const Router = require('koa-router') +const app = new Koa() +const router = new Router() +const views = require('koa-views') +app.use(require('koa-static')('public')) const port = 8080 -app.get('/', (req, res) => { - res.sendFile(`${__dirname}/comparison.html`) -}) +app.use(views(`${__dirname}`, { extension: 'html' }, {map: { handlebars: 'handlebars' }})) + +router.get('/', async ctx => await ctx.render('comparison')) -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}`)) diff --git a/exercises/02_html/04_tables/package.json b/exercises/02_html/04_tables/package.json new file mode 100644 index 0000000..f086c16 --- /dev/null +++ b/exercises/02_html/04_tables/package.json @@ -0,0 +1,17 @@ +{ + "name": "04_tables", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "koa": "^2.6.2", + "koa-router": "^7.4.0", + "koa-static": "^5.0.0", + "koa-views": "^6.1.5" + } +} diff --git a/exercises/02_html/05_semantic/index.js b/exercises/02_html/05_semantic/index.js index 8a383a3..e2b33d1 100644 --- a/exercises/02_html/05_semantic/index.js +++ b/exercises/02_html/05_semantic/index.js @@ -1,20 +1,17 @@ +#!/usr/bin/env node -'use strict' - -const express = require('express') -const app = express() -app.use(express.static('public')) - +const Koa = require('koa') +const Router = require('koa-router') +const app = new Koa() +const router = new Router() +const views = require('koa-views') +app.use(require('koa-static')('public')) const port = 8080 -app.get('/', (req, res) => { - res.sendFile(`${__dirname}/html/review.html`) -}) +app.use(views(`${__dirname}/views`, { extension: 'html' }, {map: { handlebars: 'handlebars' }})) -app.get('/cafe', (req, res) => { - res.sendFile(`${__dirname}/html/cafe.html`) -}) +router.get('/', async ctx => await ctx.render('review')) +router.get('/cafe', async ctx => ctx.render('cafe')) -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}`)) diff --git a/exercises/02_html/05_semantic/package.json b/exercises/02_html/05_semantic/package.json new file mode 100644 index 0000000..7c76ce3 --- /dev/null +++ b/exercises/02_html/05_semantic/package.json @@ -0,0 +1,17 @@ +{ + "name": "05_semantic", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "koa": "^2.6.2", + "koa-router": "^7.4.0", + "koa-static": "^5.0.0", + "koa-views": "^6.1.5" + } +} diff --git a/exercises/02_html/05_semantic/html/cafe.html b/exercises/02_html/05_semantic/views/cafe.html similarity index 100% rename from exercises/02_html/05_semantic/html/cafe.html rename to exercises/02_html/05_semantic/views/cafe.html diff --git a/exercises/02_html/05_semantic/html/review.html b/exercises/02_html/05_semantic/views/review.html similarity index 100% rename from exercises/02_html/05_semantic/html/review.html rename to exercises/02_html/05_semantic/views/review.html diff --git a/exercises/02_html/06_markdown/express.js b/exercises/02_html/06_markdown/express.js new file mode 100644 index 0000000..23d8cab --- /dev/null +++ b/exercises/02_html/06_markdown/express.js @@ -0,0 +1,23 @@ + +'use strict' + +const express = require('express') +const app = express() + +const marked = require('marked') +const fs = require('fs') + +const port = 8080 + +app.get('/', (req, res) => { + fs.readFile(`${__dirname}/computers.md`, 'utf8', (err, data) => { + if(err) { + console.log(err) + } + res.send(marked(data.toString())) + }) +}) + +app.listen(port, () => { + console.log(`app listening on port ${port}`) +}) diff --git a/exercises/02_html/06_markdown/index.js b/exercises/02_html/06_markdown/index.js index 23d8cab..60ca1b2 100644 --- a/exercises/02_html/06_markdown/index.js +++ b/exercises/02_html/06_markdown/index.js @@ -1,23 +1,16 @@ - -'use strict' - -const express = require('express') -const app = express() +#!/usr/bin/env node const marked = require('marked') -const fs = require('fs') +const readFile = require('fs-readfile-promise') +const Koa = require('koa') +const Router = require('koa-router') +const app = new Koa() +const router = new Router() +app.use(require('koa-static')('public')) const port = 8080 -app.get('/', (req, res) => { - fs.readFile(`${__dirname}/computers.md`, 'utf8', (err, data) => { - if(err) { - console.log(err) - } - res.send(marked(data.toString())) - }) -}) +router.get('/', async ctx => ctx.body = marked(await readFile('./computers.md', 'utf8'))) -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}`)) diff --git a/exercises/02_html/06_markdown/package.json b/exercises/02_html/06_markdown/package.json new file mode 100644 index 0000000..68cbafb --- /dev/null +++ b/exercises/02_html/06_markdown/package.json @@ -0,0 +1,22 @@ +{ + "name": "06_markdown", + "version": "1.0.0", + "description": "", + "main": "index.js", + "dependencies": { + "fs-extra": "^7.0.1", + "fs-readfile-promise": "^3.0.1", + "koa": "^2.6.2", + "koa-router": "^7.4.0", + "koa-static": "^5.0.0", + "koa-views": "^6.1.5", + "kow": "^3.2.3", + "marked": "^0.6.0" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +}