├── aula09-java-Async ├── file1.txt ├── file2.txt ├── file3.txt ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── App.java └── build.gradle ├── aula11-socket-client ├── file1.txt ├── file3.txt ├── file2.txt ├── target.txt ├── app10-tcp-client.js └── app10-tcp-server.js ├── aula09-files ├── target.txt ├── app06-file-read-many-Sync.js ├── app05-file-read-many.js └── .eslintrc.json ├── aula10-socket ├── target.txt └── app10-tcp-server.js ├── aula12-stream-json-parser ├── file1.txt ├── file2.txt ├── target.txt ├── file3.txt ├── package.json ├── app10-tcp-client.js ├── app11-tcp-client-json-parser.js ├── app10-tcp-Json-Flaw-server.js ├── lib │ └── stream-json-parser.js └── app10-tcp-server.js ├── aula08-nodejs-intro ├── target.txt ├── app01-event-loop-Blocking.js ├── app04-file-read.js ├── app02-event-loop.js ├── app03-files.js ├── app06-file-read-many-Sync.js ├── app05-file-read-many.js └── .eslintrc.json ├── aula19-data-project-gutenberg ├── databases │ ├── .vscode │ │ └── settings.json │ ├── 00-README.txt │ ├── package.json │ ├── rdf-to-bulk.js │ ├── lib │ │ └── parse-rdf.js │ └── test │ │ └── parse-rdf-test.js └── 00-README.txt ├── aula09-java-sync ├── App.class ├── .gitignore └── App.java ├── aula38-b4-webpack ├── app │ ├── pg.jpg │ ├── entry.js │ ├── bookSearchResults.hbs │ ├── bookSearch.js │ ├── b4index.html │ └── bookSearch.html ├── bundles-server.js ├── lib │ ├── static-files.js │ └── bundle-mock.js ├── test │ └── parallel-test.js ├── package.json ├── webpack.config.js └── bundles-web-api.js ├── aula43-b4-passport ├── app │ ├── img │ │ ├── pg.jpg │ │ └── Frogatto.png │ ├── js │ │ ├── b4index.js │ │ ├── util.js │ │ ├── bookSearch.js │ │ └── bundles.js │ └── views │ │ ├── b4index.html │ │ ├── alert.hbs │ │ ├── bundlesCreate.hbs │ │ ├── bundlesList.hbs │ │ ├── bookSearchResults.hbs │ │ ├── bookSearch.html │ │ ├── navbar.hbs │ │ ├── main.html │ │ └── login.html ├── lib │ ├── static-files.js │ ├── auth.js │ └── bundle-mock.js ├── webpack.config.js ├── test │ └── parallel-test.js ├── .vscode │ └── launch.json ├── package.json ├── bundles-server.js └── auth-web-api.js ├── aula42-b4-authentication ├── app │ ├── img │ │ ├── pg.jpg │ │ └── Frogatto.png │ ├── js │ │ ├── b4index.js │ │ ├── util.js │ │ ├── bookSearch.js │ │ ├── bundles.js │ │ └── entry.js │ └── views │ │ ├── alert.hbs │ │ ├── b4index.html │ │ ├── bundlesCreate.hbs │ │ ├── bundlesList.hbs │ │ ├── bookSearchResults.hbs │ │ ├── bookSearch.html │ │ ├── navbar.hbs │ │ └── main.html ├── lib │ ├── static-files.js │ └── bundle-mock.js ├── webpack.config.js ├── test │ └── parallel-test.js ├── .vscode │ └── launch.json ├── package.json ├── bundles-server.js └── bundles-web-api.js ├── aula39-b4-SPA-hash-based-routing ├── app │ ├── img │ │ ├── pg.jpg │ │ └── Frogatto.png │ ├── js │ │ ├── b4index.js │ │ ├── entry.js │ │ └── bookSearch.js │ └── views │ │ ├── b4index.html │ │ ├── bookSearchResults.hbs │ │ ├── bookSearch.html │ │ └── main.html ├── bundles-server.js ├── lib │ ├── static-files.js │ └── bundle-mock.js ├── webpack.config.js ├── test │ └── parallel-test.js ├── package.json └── bundles-web-api.js ├── aula40-b4-SPA-hash-based-routing ├── app │ ├── img │ │ ├── pg.jpg │ │ └── Frogatto.png │ ├── js │ │ ├── b4index.js │ │ ├── entry.js │ │ └── bookSearch.js │ └── views │ │ ├── b4index.html │ │ ├── bookSearchResults.hbs │ │ ├── bookSearch.html │ │ └── main.html ├── lib │ ├── static-files.js │ └── bundle-mock.js ├── webpack.config.js ├── test │ └── parallel-test.js ├── package.json ├── bundles-server.js └── bundles-web-api.js ├── aula16-http-and-elastic ├── task1.json └── task2.json ├── aula01-intro └── hello.js ├── aula02-scopes ├── ex01-vars.js ├── ex02-scopes.js └── .eslintrc.json ├── aula03-types ├── ex03-primitives.js ├── .eslintrc.json └── ex04-custom-types.js ├── aula36-b4-front-end-javascript ├── public │ ├── bookSearch.js │ ├── bookSearch.html │ └── b4index.html ├── bundles-server.js ├── .vscode │ └── launch.json ├── package.json ├── lib │ ├── static-files.js │ └── bundle-mock.js ├── test │ └── parallel-test.js └── bundles-web-api.js ├── aula41-cookies-session ├── package.json ├── app01-cookies.js ├── app02-cookies-parser.js └── app03-session.js ├── aula32-promises-basics ├── package.json ├── app01.js ├── test │ └── parallel-test.js └── app02-delay.js ├── aula04-functions ├── ex02-this.js ├── ex01-functions.js ├── .eslintrc.json └── ex03-funcao-construtora.js ├── aula31-b4-web-server-express ├── bundles-server.js ├── package.json ├── .vscode │ └── launch.json ├── lib │ ├── parallel.js │ └── bundle-mock.js ├── test │ └── parallel-test.js └── bundles-web-api.js ├── aula33-b4-web-server-promises ├── bundles-server.js ├── .vscode │ └── launch.json ├── package.json ├── test │ └── parallel-test.js ├── bundles-web-api.js └── lib │ └── bundle-mock.js ├── aula13-unit-tests ├── package.json ├── .vscode │ └── launch.json ├── lib │ └── stream-json-parser.js └── test │ └── stream-json-parser-test.js ├── aula22-b4-web-server ├── package.json ├── .vscode │ └── launch.json ├── bundles-server.js ├── lib │ └── bundle-mock.js └── bundles-web-api.js ├── aula28-b4-web-server-Router ├── package.json ├── .vscode │ └── launch.json ├── lib │ ├── parallel.js │ └── bundle-mock.js ├── test │ └── parallel-test.js ├── bundles-server.js └── bundles-web-api.js ├── aula37-b4-front-end-templates ├── bundles-server.js ├── public │ ├── bookSearchResults.hbs │ ├── bookSearch.js │ ├── b4index.html │ └── bookSearch.html ├── .vscode │ └── launch.json ├── package.json ├── lib │ ├── static-files.js │ └── bundle-mock.js ├── test │ └── parallel-test.js └── bundles-web-api.js ├── aula35-b4-static-files ├── .vscode │ └── launch.json ├── bundles-server.js ├── package.json ├── lib │ ├── static-files.js │ └── bundle-mock.js ├── test │ └── parallel-test.js ├── bundles-web-api.js └── public │ ├── bookSearch.html │ └── b4index.html ├── aula21-b4-addBook-to-Bundle └── package.json ├── aula20-b4-Better-Book-Bundle-Builder ├── package.json ├── test │ └── bundle-test.js └── lib │ └── bundle.js ├── aula07-prototype ├── .eslintrc.json ├── ex01-prototype.js └── ex02-inheritance.js ├── .eslintrc.json ├── .gitignore └── aula34-html-csss └── bookSearch.html /aula09-java-Async/file1.txt: -------------------------------------------------------------------------------- 1 | Ola -------------------------------------------------------------------------------- /aula09-java-Async/file2.txt: -------------------------------------------------------------------------------- 1 | ole -------------------------------------------------------------------------------- /aula09-java-Async/file3.txt: -------------------------------------------------------------------------------- 1 | hello -------------------------------------------------------------------------------- /aula11-socket-client/file1.txt: -------------------------------------------------------------------------------- 1 | Ola -------------------------------------------------------------------------------- /aula09-files/target.txt: -------------------------------------------------------------------------------- 1 | dff 2 | fsadf -------------------------------------------------------------------------------- /aula10-socket/target.txt: -------------------------------------------------------------------------------- 1 | kçfkdç 2 | -------------------------------------------------------------------------------- /aula11-socket-client/file3.txt: -------------------------------------------------------------------------------- 1 | hello -------------------------------------------------------------------------------- /aula12-stream-json-parser/file1.txt: -------------------------------------------------------------------------------- 1 | Ola -------------------------------------------------------------------------------- /aula08-nodejs-intro/target.txt: -------------------------------------------------------------------------------- 1 | dff 2 | fsadf -------------------------------------------------------------------------------- /aula11-socket-client/file2.txt: -------------------------------------------------------------------------------- 1 | ole 2 | 3 | -------------------------------------------------------------------------------- /aula11-socket-client/target.txt: -------------------------------------------------------------------------------- 1 | kçfkdç 2 | -------------------------------------------------------------------------------- /aula12-stream-json-parser/file2.txt: -------------------------------------------------------------------------------- 1 | ole 2 | 3 | -------------------------------------------------------------------------------- /aula12-stream-json-parser/target.txt: -------------------------------------------------------------------------------- 1 | kçfkdç 2 | -------------------------------------------------------------------------------- /aula12-stream-json-parser/file3.txt: -------------------------------------------------------------------------------- 1 | hello 2 | 3 | -------------------------------------------------------------------------------- /aula19-data-project-gutenberg/databases/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "git.ignoreLimitWarning": true 3 | } -------------------------------------------------------------------------------- /aula09-java-sync/App.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isel-leic-pi/PI-1819i-LI51N/HEAD/aula09-java-sync/App.class -------------------------------------------------------------------------------- /aula38-b4-webpack/app/pg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isel-leic-pi/PI-1819i-LI51N/HEAD/aula38-b4-webpack/app/pg.jpg -------------------------------------------------------------------------------- /aula43-b4-passport/app/img/pg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isel-leic-pi/PI-1819i-LI51N/HEAD/aula43-b4-passport/app/img/pg.jpg -------------------------------------------------------------------------------- /aula42-b4-authentication/app/img/pg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isel-leic-pi/PI-1819i-LI51N/HEAD/aula42-b4-authentication/app/img/pg.jpg -------------------------------------------------------------------------------- /aula43-b4-passport/app/img/Frogatto.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isel-leic-pi/PI-1819i-LI51N/HEAD/aula43-b4-passport/app/img/Frogatto.png -------------------------------------------------------------------------------- /aula38-b4-webpack/app/entry.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | require('./../node_modules/bootstrap/dist/css/bootstrap.css') 4 | 5 | require('./bookSearch.js') -------------------------------------------------------------------------------- /aula42-b4-authentication/app/img/Frogatto.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isel-leic-pi/PI-1819i-LI51N/HEAD/aula42-b4-authentication/app/img/Frogatto.png -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/app/img/pg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isel-leic-pi/PI-1819i-LI51N/HEAD/aula39-b4-SPA-hash-based-routing/app/img/pg.jpg -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/app/img/pg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isel-leic-pi/PI-1819i-LI51N/HEAD/aula40-b4-SPA-hash-based-routing/app/img/pg.jpg -------------------------------------------------------------------------------- /aula09-java-Async/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isel-leic-pi/PI-1819i-LI51N/HEAD/aula09-java-Async/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/app/img/Frogatto.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isel-leic-pi/PI-1819i-LI51N/HEAD/aula39-b4-SPA-hash-based-routing/app/img/Frogatto.png -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/app/img/Frogatto.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isel-leic-pi/PI-1819i-LI51N/HEAD/aula40-b4-SPA-hash-based-routing/app/img/Frogatto.png -------------------------------------------------------------------------------- /aula16-http-and-elastic/task1.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "task01", 3 | "title": "Swimming", 4 | "description": "Swim workout on pool with 50 meters length", 5 | "location": "Olivais" 6 | } 7 | -------------------------------------------------------------------------------- /aula16-http-and-elastic/task2.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "task02", 3 | "title": "Study", 4 | "description": "Study web programming with node.js", 5 | "location": "ISEL or at home" 6 | } 7 | -------------------------------------------------------------------------------- /aula43-b4-passport/app/js/b4index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const b4indexView = require('./../views/b4index.html') 4 | 5 | module.exports = (divMain) => { 6 | divMain.innerHTML = b4indexView 7 | } -------------------------------------------------------------------------------- /aula08-nodejs-intro/app01-event-loop-Blocking.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | let stop = false 4 | 5 | setTimeout(() => stop = true, 1000) // 1000 ms 6 | 7 | while(!stop) { 8 | console.log('Waiting...') 9 | } -------------------------------------------------------------------------------- /aula08-nodejs-intro/app04-file-read.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | fs.readFile('target.txt', (err, data) => { 6 | if (err) throw err 7 | console.log(data.toString()) 8 | }) -------------------------------------------------------------------------------- /aula42-b4-authentication/app/js/b4index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const b4indexView = require('./../views/b4index.html') 4 | 5 | module.exports = (divMain) => { 6 | divMain.innerHTML = b4indexView 7 | } -------------------------------------------------------------------------------- /aula01-intro/hello.js: -------------------------------------------------------------------------------- 1 | 2 | console.log('hello') 3 | 4 | function printHello() { 5 | x = 7 6 | } 7 | 8 | 9 | function dummy(y){ 10 | console.log(x) 11 | } 12 | 13 | printHello() 14 | dummy() -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/app/js/b4index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const b4indexView = require('./../views/b4index.html') 4 | 5 | module.exports = (divMain) => { 6 | divMain.innerHTML = b4indexView 7 | } -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/app/js/b4index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const b4indexView = require('./../views/b4index.html') 4 | 5 | module.exports = (divMain) => { 6 | divMain.innerHTML = b4indexView 7 | } -------------------------------------------------------------------------------- /aula02-scopes/ex01-vars.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | console.log('hello') 4 | 5 | function printHello() { 6 | x = 7 7 | return x 8 | } 9 | 10 | 11 | function dummy(y){ 12 | console.log(x) 13 | } 14 | 15 | printHello() 16 | dummy() -------------------------------------------------------------------------------- /aula02-scopes/ex02-scopes.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function f(x) { 4 | if(x == 7) 5 | { 6 | let i 7 | for(i = 0; i <= x; i++) { 8 | console.log(i) 9 | } 10 | console.log(i) 11 | } 12 | } 13 | 14 | f(7) -------------------------------------------------------------------------------- /aula09-java-Async/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /aula43-b4-passport/app/views/b4index.html: -------------------------------------------------------------------------------- 1 |

Better Book Bundle Builder

2 |

3 | A catalog of 57,000 books from Project Gutenberg. 4 | Moreover, it allows to create groups of books, named bundles, 5 | that aggregate a set of books. 6 |

7 | -------------------------------------------------------------------------------- /aula19-data-project-gutenberg/00-README.txt: -------------------------------------------------------------------------------- 1 | # Place on data folder the XML data files from project gutenberg database. 2 | # Run the following commands: 3 | 4 | $ cd data 5 | $ curl -O http://www.gutenberg.org/cache/epub/feeds/rdf-files.tar.bz2 6 | $ tar -xvjf rdf-files.tar.bz2 7 | 8 | -------------------------------------------------------------------------------- /aula43-b4-passport/app/views/alert.hbs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /aula42-b4-authentication/app/views/alert.hbs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /aula42-b4-authentication/app/views/b4index.html: -------------------------------------------------------------------------------- 1 |

Better Book Bundle Builder

2 |

3 | A catalog of 57,000 books from Project Gutenberg. 4 | Moreover, it allows to create groups of books, named bundles, 5 | that aggregate a set of books. 6 |

7 | -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/app/views/b4index.html: -------------------------------------------------------------------------------- 1 |

Better Book Bundle Builder

2 |

3 | A catalog of 57,000 books from Project Gutenberg. 4 | Moreover, it allows to create groups of books, named bundles, 5 | that aggregate a set of books. 6 |

7 | -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/app/views/b4index.html: -------------------------------------------------------------------------------- 1 |

Better Book Bundle Builder

2 |

3 | A catalog of 57,000 books from Project Gutenberg. 4 | Moreover, it allows to create groups of books, named bundles, 5 | that aggregate a set of books. 6 |

7 | -------------------------------------------------------------------------------- /aula03-types/ex03-primitives.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function printType(val) { 4 | const str = typeof(val) 5 | console.log(str) 6 | } 7 | 8 | printType('ola') 9 | printType(723) 10 | printType(true) 11 | printType() 12 | printType(new Object()) 13 | printType(printType) 14 | printType({}) 15 | -------------------------------------------------------------------------------- /aula08-nodejs-intro/app02-event-loop.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | let stop = false 4 | 5 | setTimeout(() => stop = true, 1000) // 1000 ms 6 | 7 | checkStop() 8 | 9 | function checkStop() { 10 | if(!stop) { 11 | console.log('Waiting...') 12 | setTimeout(checkStop, 100) 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /aula43-b4-passport/app/views/bundlesCreate.hbs: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 |
5 | 6 |
-------------------------------------------------------------------------------- /aula42-b4-authentication/app/views/bundlesCreate.hbs: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 |
5 | 6 |
-------------------------------------------------------------------------------- /aula36-b4-front-end-javascript/public/bookSearch.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | fetch('http://localhost:3000/api/books/search?title=djfhd&authors=Twain') 4 | .then(res => res.json()) 5 | .then(arr => { 6 | console.log(arr[1].title) 7 | document.body.innerHTML += JSON.stringify(arr[1]) 8 | }) 9 | .catch(err => console.log(err)) -------------------------------------------------------------------------------- /aula12-stream-json-parser/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula12-stream-json-parser", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app10-tcp-Json-Flaw-server.js", 6 | "dependencies": { 7 | }, 8 | "scripts": {}, 9 | "author": "", 10 | "license": "ISC", 11 | "devDependencies": { 12 | "mocha": "3.4.2" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /aula08-nodejs-intro/app03-files.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | if(process.argv.length == 2) 6 | throw new Error('You must provide a filename!') 7 | 8 | const filename = process.argv[2] 9 | 10 | fs.watch(filename, (ev) => console.log(`File ${filename} changed! ${ev}`)) 11 | console.log(`Now watching ${filename} for changes...`) 12 | 13 | -------------------------------------------------------------------------------- /aula41-cookies-session/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "app02-cookies-parser", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "cookie-parser": "^1.4.3", 13 | "express": "^4.15.2", 14 | "express-session": "^1.15.6" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /aula43-b4-passport/app/views/bundlesList.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | {{#each bundles}} 10 | 11 | 12 | 13 | 14 | {{/each}} 15 | 16 |
IdName
{{_id}}{{name}}
17 | -------------------------------------------------------------------------------- /aula32-promises-basics/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula32-promises-basics", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app01.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "author": "", 13 | "license": "ISC", 14 | "dependencies": { 15 | "chai": "^4.2.0", 16 | "mocha": "^5.2.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /aula42-b4-authentication/app/views/bundlesList.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | {{#each bundles}} 10 | 11 | 12 | 13 | 14 | {{/each}} 15 | 16 |
IdName
{{_id}}{{name}}
17 | -------------------------------------------------------------------------------- /aula04-functions/ex02-this.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function makeStudent(nr, fullname) { 4 | return { 5 | nr, // <=> 'nr': nr, 6 | fullname, 7 | // !!!! ALERTA !!!! : toString: () => this.nr + ': ' + this.fullname 8 | toString: function(){ return this.nr + ': ' + this.fullname } 9 | } 10 | } 11 | 12 | const std = makeStudent(83615, 'Ze Manel') 13 | console.log(std.toString()) 14 | console.log(std) 15 | 16 | -------------------------------------------------------------------------------- /aula31-b4-web-server-express/bundles-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const http = require('http') 4 | const express = require('express') 5 | const morgan = require('morgan') 6 | const bundlesWebApi = require('./bundles-web-api') 7 | 8 | const webServer = express() 9 | webServer.use(morgan('dev')) 10 | bundlesWebApi(webServer) 11 | http 12 | .createServer(webServer) 13 | .listen(3000, () => console.log('Server running on port 3000')) 14 | -------------------------------------------------------------------------------- /aula33-b4-web-server-promises/bundles-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const http = require('http') 4 | const express = require('express') 5 | const morgan = require('morgan') 6 | const bundlesWebApi = require('./bundles-web-api') 7 | 8 | const webServer = express() 9 | webServer.use(morgan('dev')) 10 | bundlesWebApi(webServer) 11 | http 12 | .createServer(webServer) 13 | .listen(3000, () => console.log('Server running on port 3000')) 14 | -------------------------------------------------------------------------------- /aula13-unit-tests/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula13-unit-tests", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "directories": { 7 | "lib": "lib" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "keywords": [], 13 | "author": "", 14 | "license": "ISC", 15 | "devDependencies": { 16 | "chai": "3.5.0", 17 | "mocha": "3.4.2" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /aula22-b4-web-server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula22-b4-web-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bundles-server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "request": "^2.88.0" 13 | }, 14 | "devDependencies": { 15 | "chai": "^4.2.0", 16 | "mocha": "^5.2.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /aula28-b4-web-server-Router/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula22-b4-web-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bundles-server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "request": "^2.88.0" 13 | }, 14 | "devDependencies": { 15 | "chai": "^4.2.0", 16 | "mocha": "^5.2.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /aula19-data-project-gutenberg/databases/00-README.txt: -------------------------------------------------------------------------------- 1 | ## 2 | ## Run the following command to convert XML from ../data/cache/epub/ to JSON. 3 | ## 4 | 5 | node rdf-to-bulk.js ../data/cache/epub/ > ../data/bulk_pg.ldj 6 | 7 | ## 8 | ## Insert previous JSON documents from bulk_pg.ldj into ElasticSearch database. 9 | ## 10 | 11 | curl -X POST 12 | -H "Content-Type: application/json" 13 | --data-binary "@bulk_pg.json" 14 | http://localhost:9200/books/book/_bulk` -------------------------------------------------------------------------------- /aula38-b4-webpack/bundles-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const http = require('http') 4 | const express = require('express') 5 | const morgan = require('morgan') 6 | const bundlesWebApi = require('./bundles-web-api') 7 | 8 | const webServer = express() 9 | webServer.use(morgan('dev')) 10 | webServer.use(express.static('dist')) 11 | bundlesWebApi(webServer) 12 | http 13 | .createServer(webServer) 14 | .listen(3000, () => console.log('Server running on port 3000')) 15 | -------------------------------------------------------------------------------- /aula36-b4-front-end-javascript/bundles-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const http = require('http') 4 | const express = require('express') 5 | const morgan = require('morgan') 6 | const bundlesWebApi = require('./bundles-web-api') 7 | 8 | const webServer = express() 9 | webServer.use(morgan('dev')) 10 | webServer.use(express.static('public')) 11 | bundlesWebApi(webServer) 12 | http 13 | .createServer(webServer) 14 | .listen(3000, () => console.log('Server running on port 3000')) 15 | -------------------------------------------------------------------------------- /aula37-b4-front-end-templates/bundles-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const http = require('http') 4 | const express = require('express') 5 | const morgan = require('morgan') 6 | const bundlesWebApi = require('./bundles-web-api') 7 | 8 | const webServer = express() 9 | webServer.use(morgan('dev')) 10 | webServer.use(express.static('public')) 11 | bundlesWebApi(webServer) 12 | http 13 | .createServer(webServer) 14 | .listen(3000, () => console.log('Server running on port 3000')) 15 | -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/bundles-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const http = require('http') 4 | const express = require('express') 5 | const morgan = require('morgan') 6 | const bundlesWebApi = require('./bundles-web-api') 7 | 8 | const webServer = express() 9 | webServer.use(morgan('dev')) 10 | webServer.use(express.static('dist')) 11 | bundlesWebApi(webServer) 12 | http 13 | .createServer(webServer) 14 | .listen(3000, () => console.log('Server running on port 3000')) 15 | -------------------------------------------------------------------------------- /aula31-b4-web-server-express/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula22-b4-web-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bundles-server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "express": "^4.16.4", 13 | "request": "^2.88.0" 14 | }, 15 | "devDependencies": { 16 | "chai": "^4.2.0", 17 | "mocha": "^5.2.0" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /aula38-b4-webpack/app/bookSearchResults.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {{#each books}} 11 | 12 | 13 | 14 | 15 | 16 | {{/each}} 17 | 18 |
TitleAuthorsSubjects
{{title}}{{authors}}{{subjects}}
19 | -------------------------------------------------------------------------------- /aula09-files/app06-file-read-many-Sync.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | if(process.argv.length == 2) 6 | throw new Error('You must provide filenames!') 7 | 8 | 9 | process 10 | .argv 11 | .slice(2) 12 | .forEach(filename => { 13 | console.log('Reading ' + filename) 14 | const data = fs.readFileSync(filename) 15 | console.log('##########################################################') 16 | console.log(data.toString()) 17 | }) -------------------------------------------------------------------------------- /aula43-b4-passport/app/views/bookSearchResults.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {{#each books}} 11 | 12 | 13 | 14 | 15 | 16 | {{/each}} 17 | 18 |
TitleAuthorsSubjects
{{title}}{{authors}}{{subjects}}
19 | -------------------------------------------------------------------------------- /aula42-b4-authentication/app/views/bookSearchResults.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {{#each books}} 11 | 12 | 13 | 14 | 15 | 16 | {{/each}} 17 | 18 |
TitleAuthorsSubjects
{{title}}{{authors}}{{subjects}}
19 | -------------------------------------------------------------------------------- /aula32-promises-basics/app01.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const ok = Promise.resolve().then(() => 5) 4 | const err = Promise.resolve().then(() => { throw Error('Invalid result!')}) 5 | 6 | const ok2 = (async () => 7)() 7 | const err2 = (async () => { throw Error('Bad result!')})() 8 | 9 | fwdResult(ok) 10 | fwdResult(ok2) 11 | fwdResult(err) 12 | fwdResult(err2) 13 | 14 | function fwdResult(prm) { 15 | prm 16 | .then(res => console.log(res)) 17 | .catch(err => console.log(err.message)) 18 | } 19 | -------------------------------------------------------------------------------- /aula37-b4-front-end-templates/public/bookSearchResults.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {{#each books}} 11 | 12 | 13 | 14 | 15 | 16 | {{/each}} 17 | 18 |
TitleAuthorsSubjects
{{title}}{{authors}}{{subjects}}
19 | -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/app/views/bookSearchResults.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {{#each books}} 11 | 12 | 13 | 14 | 15 | 16 | {{/each}} 17 | 18 |
TitleAuthorsSubjects
{{title}}{{authors}}{{subjects}}
19 | -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/app/views/bookSearchResults.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {{#each books}} 11 | 12 | 13 | 14 | 15 | 16 | {{/each}} 17 | 18 |
TitleAuthorsSubjects
{{title}}{{authors}}{{subjects}}
19 | -------------------------------------------------------------------------------- /aula19-data-project-gutenberg/databases/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "databases", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha", 8 | "test:watch": "mocha --watch --reporter min" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "devDependencies": { 14 | "chai": "3.5.0", 15 | "mocha": "2.4.5" 16 | }, 17 | "dependencies": { 18 | "cheerio": "0.22.0", 19 | "node-dir": "^0.1.17" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /aula22-b4-web-server/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Launch Program", 11 | "program": "${workspaceFolder}\\bundles-server.js" 12 | } 13 | ] 14 | } -------------------------------------------------------------------------------- /aula35-b4-static-files/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Launch Program", 11 | "program": "${workspaceFolder}\\bundles-server.js" 12 | } 13 | ] 14 | } -------------------------------------------------------------------------------- /aula28-b4-web-server-Router/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Launch Program", 11 | "program": "${workspaceFolder}\\bundles-server.js" 12 | } 13 | ] 14 | } -------------------------------------------------------------------------------- /aula35-b4-static-files/bundles-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const http = require('http') 4 | const express = require('express') 5 | const morgan = require('morgan') 6 | const bundlesWebApi = require('./bundles-web-api') 7 | const staticFiles = require('./lib/static-files') 8 | 9 | const webServer = express() 10 | webServer.use(morgan('dev')) 11 | webServer.use(staticFiles('public')) 12 | bundlesWebApi(webServer) 13 | http 14 | .createServer(webServer) 15 | .listen(3000, () => console.log('Server running on port 3000')) 16 | -------------------------------------------------------------------------------- /aula31-b4-web-server-express/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Launch Program", 11 | "program": "${workspaceFolder}\\bundles-server.js" 12 | } 13 | ] 14 | } -------------------------------------------------------------------------------- /aula33-b4-web-server-promises/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Launch Program", 11 | "program": "${workspaceFolder}\\bundles-server.js" 12 | } 13 | ] 14 | } -------------------------------------------------------------------------------- /aula36-b4-front-end-javascript/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Launch Program", 11 | "program": "${workspaceFolder}\\bundles-server.js" 12 | } 13 | ] 14 | } -------------------------------------------------------------------------------- /aula37-b4-front-end-templates/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Launch Program", 11 | "program": "${workspaceFolder}\\bundles-server.js" 12 | } 13 | ] 14 | } -------------------------------------------------------------------------------- /aula09-files/app05-file-read-many.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | if(process.argv.length == 2) 6 | throw new Error('You must provide filenames!') 7 | 8 | process 9 | .argv 10 | .slice(2) 11 | .forEach(filename => { 12 | console.log('Reading ' + filename) 13 | fs.readFile(filename, cb) 14 | }) 15 | 16 | function cb(err, data) { 17 | if (err) throw err 18 | console.log('##########################################################') 19 | console.log(data.toString()) 20 | } -------------------------------------------------------------------------------- /aula41-cookies-session/app01-cookies.js: -------------------------------------------------------------------------------- 1 | const http = require('http') 2 | 3 | const bag = { 4 | items: ['iphone', 'laptop', 'bike'], 5 | total: 1750 6 | } 7 | 8 | const app = (req, res) => { 9 | console.log(req.headers.cookie) 10 | //////////////////////////////////////////////////// 11 | res.writeHead(200, [ 12 | ['Set-Cookie', 'username=fmcarvalho'], 13 | ['Set-Cookie', 'bag=' + JSON.stringify(bag)] 14 | ]) 15 | res.end() 16 | } 17 | 18 | const server = http.createServer(app) 19 | server.listen(3000) -------------------------------------------------------------------------------- /aula08-nodejs-intro/app06-file-read-many-Sync.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | if(process.argv.length == 2) 6 | throw new Error('You must provide filenames!') 7 | 8 | const args = process.argv 9 | for (let index = 2; index < args.length; index++) { 10 | const filename = args[index] 11 | console.log('Reading ' + filename) 12 | const data = fs.readFileSync(filename) 13 | console.log('##########################################################') 14 | console.log(data.toString()) 15 | } 16 | 17 | 18 | -------------------------------------------------------------------------------- /aula21-b4-addBook-to-Bundle/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula20-b4-better-book-bundle-builder", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "directories": { 7 | "lib": "lib", 8 | "test": "test" 9 | }, 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "author": "", 14 | "license": "ISC", 15 | "dependencies": { 16 | "request": "^2.88.0" 17 | }, 18 | "devDependencies": { 19 | "chai": "^4.2.0", 20 | "mocha": "^5.2.0" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /aula35-b4-static-files/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula22-b4-web-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bundles-server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "express": "^4.16.4", 13 | "morgan": "^1.9.1", 14 | "request": "^2.88.0", 15 | "request-promise": "^4.2.2" 16 | }, 17 | "devDependencies": { 18 | "chai": "^4.2.0", 19 | "mocha": "^5.2.0" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /aula20-b4-Better-Book-Bundle-Builder/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula20-b4-better-book-bundle-builder", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "directories": { 7 | "lib": "lib", 8 | "test": "test" 9 | }, 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "author": "", 14 | "license": "ISC", 15 | "dependencies": { 16 | "request": "^2.88.0" 17 | }, 18 | "devDependencies": { 19 | "chai": "^4.2.0", 20 | "mocha": "^5.2.0" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /aula33-b4-web-server-promises/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula22-b4-web-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bundles-server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "express": "^4.16.4", 13 | "morgan": "^1.9.1", 14 | "request": "^2.88.0", 15 | "request-promise": "^4.2.2" 16 | }, 17 | "devDependencies": { 18 | "chai": "^4.2.0", 19 | "mocha": "^5.2.0" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /aula37-b4-front-end-templates/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula22-b4-web-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bundles-server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "express": "^4.16.4", 13 | "morgan": "^1.9.1", 14 | "request": "^2.88.0", 15 | "request-promise": "^4.2.2" 16 | }, 17 | "devDependencies": { 18 | "chai": "^4.2.0", 19 | "mocha": "^5.2.0" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /aula36-b4-front-end-javascript/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula22-b4-web-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bundles-server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "express": "^4.16.4", 13 | "morgan": "^1.9.1", 14 | "request": "^2.88.0", 15 | "request-promise": "^4.2.2" 16 | }, 17 | "devDependencies": { 18 | "chai": "^4.2.0", 19 | "mocha": "^5.2.0" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /aula09-java-Async/.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | .vertx 3 | build/ 4 | 5 | # Ignore InteliJ metadata 6 | .idea 7 | *.iml 8 | 9 | # Ignore InteliJ build folder 10 | 11 | out/ 12 | 13 | # Ignore Gradle GUI config 14 | gradle-app.setting 15 | 16 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 17 | !gradle-wrapper.jar 18 | 19 | # Cache of project 20 | .gradletasknamecache 21 | 22 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 23 | # gradle/wrapper/gradle-wrapper.properties 24 | 25 | # Ignore world Weather API key 26 | worldweatheronline-app-key.txt -------------------------------------------------------------------------------- /aula09-java-sync/.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | .vertx 3 | build/ 4 | 5 | # Ignore InteliJ metadata 6 | .idea 7 | *.iml 8 | 9 | # Ignore InteliJ build folder 10 | 11 | out/ 12 | 13 | # Ignore Gradle GUI config 14 | gradle-app.setting 15 | 16 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 17 | !gradle-wrapper.jar 18 | 19 | # Cache of project 20 | .gradletasknamecache 21 | 22 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 23 | # gradle/wrapper/gradle-wrapper.properties 24 | 25 | # Ignore world Weather API key 26 | worldweatheronline-app-key.txt -------------------------------------------------------------------------------- /aula04-functions/ex01-functions.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function bar(arg1, arg2) { 4 | console.log('I am Bar: ' + arg1 + ', ' + arg2) 5 | let str = '( this = ' + this + ', ' 6 | for (let index = 0; index < arguments.length; index++) { 7 | const element = arguments[index]; 8 | str += element + ', ' 9 | } 10 | str += ')' 11 | console.log(str) 12 | } 13 | 14 | const foo = bar 15 | 16 | console.log(bar.name) // bar 17 | console.log(foo.name) // bar 18 | 19 | 20 | foo('ola') 21 | bar(5, 7) 22 | 23 | const obj = {} 24 | obj.taz = bar 25 | obj.taz(true, 'isel') -------------------------------------------------------------------------------- /aula08-nodejs-intro/app05-file-read-many.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | if(process.argv.length == 2) 6 | throw new Error('You must provide filenames!') 7 | 8 | const args = process.argv 9 | for (let index = 2; index < args.length; index++) { 10 | const filename = args[index] 11 | console.log('Reading ' + filename) 12 | fs.readFile(filename, (err, data) => { 13 | if (err) throw err 14 | console.log('##########################################################') 15 | console.log(data.toString()) 16 | }) 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /aula02-scopes/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es6": true, 4 | "node": true 5 | }, 6 | "extends": "eslint:recommended", 7 | "parserOptions": { 8 | "sourceType": "module" 9 | }, 10 | "rules": { 11 | "indent": [ 12 | "error", 13 | 4 14 | ], 15 | "linebreak-style": [ 16 | "error", 17 | "windows" 18 | ], 19 | "quotes": [ 20 | "error", 21 | "single" 22 | ], 23 | "semi": [ 24 | "error", 25 | "never" 26 | ], 27 | "no-console":"off" 28 | } 29 | } -------------------------------------------------------------------------------- /aula03-types/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es6": true, 4 | "node": true 5 | }, 6 | "extends": "eslint:recommended", 7 | "parserOptions": { 8 | "sourceType": "module" 9 | }, 10 | "rules": { 11 | "indent": [ 12 | "error", 13 | 4 14 | ], 15 | "linebreak-style": [ 16 | "error", 17 | "windows" 18 | ], 19 | "quotes": [ 20 | "error", 21 | "single" 22 | ], 23 | "semi": [ 24 | "error", 25 | "never" 26 | ], 27 | "no-console":"off" 28 | } 29 | } -------------------------------------------------------------------------------- /aula09-files/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es6": true, 4 | "node": true 5 | }, 6 | "extends": "eslint:recommended", 7 | "parserOptions": { 8 | "sourceType": "module" 9 | }, 10 | "rules": { 11 | "indent": [ 12 | "error", 13 | 4 14 | ], 15 | "linebreak-style": [ 16 | "error", 17 | "windows" 18 | ], 19 | "quotes": [ 20 | "error", 21 | "single" 22 | ], 23 | "semi": [ 24 | "error", 25 | "never" 26 | ], 27 | "no-console":"off" 28 | } 29 | } -------------------------------------------------------------------------------- /aula04-functions/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es6": true, 4 | "node": true 5 | }, 6 | "extends": "eslint:recommended", 7 | "parserOptions": { 8 | "sourceType": "module" 9 | }, 10 | "rules": { 11 | "indent": [ 12 | "error", 13 | 4 14 | ], 15 | "linebreak-style": [ 16 | "error", 17 | "windows" 18 | ], 19 | "quotes": [ 20 | "error", 21 | "single" 22 | ], 23 | "semi": [ 24 | "error", 25 | "never" 26 | ], 27 | "no-console":"off" 28 | } 29 | } -------------------------------------------------------------------------------- /aula07-prototype/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es6": true, 4 | "node": true 5 | }, 6 | "extends": "eslint:recommended", 7 | "parserOptions": { 8 | "sourceType": "module" 9 | }, 10 | "rules": { 11 | "indent": [ 12 | "error", 13 | 4 14 | ], 15 | "linebreak-style": [ 16 | "error", 17 | "windows" 18 | ], 19 | "quotes": [ 20 | "error", 21 | "single" 22 | ], 23 | "semi": [ 24 | "error", 25 | "never" 26 | ], 27 | "no-console":"off" 28 | } 29 | } -------------------------------------------------------------------------------- /aula08-nodejs-intro/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es6": true, 4 | "node": true 5 | }, 6 | "extends": "eslint:recommended", 7 | "parserOptions": { 8 | "sourceType": "module" 9 | }, 10 | "rules": { 11 | "indent": [ 12 | "error", 13 | 4 14 | ], 15 | "linebreak-style": [ 16 | "error", 17 | "windows" 18 | ], 19 | "quotes": [ 20 | "error", 21 | "single" 22 | ], 23 | "semi": [ 24 | "error", 25 | "never" 26 | ], 27 | "no-console":"off" 28 | } 29 | } -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es6": true, 4 | "node": true, 5 | "browser": true, 6 | "mocha":true 7 | }, 8 | "extends": "eslint:recommended", 9 | "parserOptions": { 10 | "sourceType": "module" 11 | }, 12 | "rules": { 13 | "indent": [ 14 | "error", 15 | 4, 16 | {"SwitchCase": 1} 17 | ], 18 | "linebreak-style": [ 19 | "error", 20 | "windows" 21 | ], 22 | "quotes": [ 23 | "error", 24 | "single" 25 | ], 26 | "semi": [ 27 | "error", 28 | "never" 29 | ], 30 | "no-console":"off" 31 | } 32 | } -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/app/js/entry.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | require('./../../node_modules/bootstrap/dist/css/bootstrap.css') 4 | const bookSearch = require('./bookSearch') 5 | const b4index = require('./b4index') 6 | const mainView = require('./../views/main.html') 7 | 8 | document.body.innerHTML = mainView 9 | const divMain = document.getElementById('divMain') 10 | 11 | window.onhashchange = showView 12 | window.onload = showView 13 | 14 | function showView() { 15 | const path = window.location.hash 16 | switch(path) { 17 | case '#b4index': 18 | b4index(divMain) 19 | break 20 | case '#bookSearch': 21 | bookSearch(divMain) 22 | break 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /aula28-b4-web-server-Router/lib/parallel.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | module.exports = parallel 4 | 5 | /** 6 | * 7 | * @param {Array} tasks Each element is a function (cb) => void 8 | * @param {*} cb Callback function: (err, results) => void. Parameter results is an 9 | * array where each element is the result of the corresponding task in parameter tasks. 10 | */ 11 | function parallel(tasks, cb){ 12 | let count = 0 13 | const res = [] 14 | tasks.forEach((task, i) => { 15 | task((err, data) => { 16 | if(err) 17 | return cb(err) 18 | res[i] = data 19 | count++ 20 | if(count == tasks.length){ 21 | cb(null,res) 22 | } 23 | }) 24 | }) 25 | } -------------------------------------------------------------------------------- /aula28-b4-web-server-Router/test/parallel-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | const parallel = require('./../lib/parallel') 5 | 6 | describe('Test parallel function', () => { 7 | 8 | it('Should execute two tasks with success', done => { 9 | const tasks = [ 10 | cb => setTimeout(() => cb(null, 'one'), 200), 11 | cb => setTimeout(() => cb(null, 'two'), 100) 12 | ] 13 | parallel(tasks, (err, results) => { 14 | expect(results) 15 | .to.be.an('array') 16 | .with.length(2) 17 | expect(results[0]).to.equal('one') 18 | expect(results[1]).to.equal('two') 19 | done() 20 | }) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /aula31-b4-web-server-express/lib/parallel.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | module.exports = parallel 4 | 5 | /** 6 | * 7 | * @param {Array} tasks Each element is a function (cb) => void 8 | * @param {*} cb Callback function: (err, results) => void. Parameter results is an 9 | * array where each element is the result of the corresponding task in parameter tasks. 10 | */ 11 | function parallel(tasks, cb){ 12 | let count = 0 13 | const res = [] 14 | tasks.forEach((task, i) => { 15 | task((err, data) => { 16 | if(err) 17 | return cb(err) 18 | res[i] = data 19 | count++ 20 | if(count == tasks.length){ 21 | cb(null,res) 22 | } 23 | }) 24 | }) 25 | } -------------------------------------------------------------------------------- /aula31-b4-web-server-express/test/parallel-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | const parallel = require('./../lib/parallel') 5 | 6 | describe('Test parallel function', () => { 7 | 8 | it('Should execute two tasks with success', done => { 9 | const tasks = [ 10 | cb => setTimeout(() => cb(null, 'one'), 200), 11 | cb => setTimeout(() => cb(null, 'two'), 100) 12 | ] 13 | parallel(tasks, (err, results) => { 14 | expect(results) 15 | .to.be.an('array') 16 | .with.length(2) 17 | expect(results[0]).to.equal('one') 18 | expect(results[1]).to.equal('two') 19 | done() 20 | }) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /aula04-functions/ex03-funcao-construtora.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function Person(fullname, address) { 4 | this.fullname = fullname 5 | this.address = address 6 | } 7 | 8 | /** 9 | * Student ----|> Person 10 | * Inherits fullname and address 11 | */ 12 | function Student(fullname, address, nr) { 13 | const args = [fullname, address] 14 | Person.apply(this, args) // <=> invoke(target, new Object[]{..., ...}) 15 | this.nr = nr 16 | this.toString = function() { 17 | return this.nr + ': ' + this.fullname 18 | } 19 | // !!! ALERTA não retornar nada numa função construtora !!!! 20 | } 21 | 22 | const std = new Student('Ze Manel', 'Rua Papoilas', 6575) 23 | console.log(std.toString()) 24 | console.log(std) 25 | console.log(std instanceof Student) 26 | -------------------------------------------------------------------------------- /aula38-b4-webpack/lib/static-files.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | module.exports = staticFiles 6 | 7 | /** 8 | * 9 | * @param {*} location The path where are stored the static files. 10 | */ 11 | function staticFiles(location) { 12 | return (req, res, next) => { 13 | if(req.path.indexOf('.') < 0) // If not requesting a static file 14 | return next() 15 | // Read a file from disk and send it to the response 16 | const path = location + req.path 17 | fs.stat(path, (err) => { 18 | if(!err){ 19 | // Falta o content-type 20 | fs 21 | .createReadStream(path) 22 | .pipe(res) 23 | } 24 | }) 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /aula43-b4-passport/lib/static-files.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | module.exports = staticFiles 6 | 7 | /** 8 | * 9 | * @param {*} location The path where are stored the static files. 10 | */ 11 | function staticFiles(location) { 12 | return (req, res, next) => { 13 | if(req.path.indexOf('.') < 0) // If not requesting a static file 14 | return next() 15 | // Read a file from disk and send it to the response 16 | const path = location + req.path 17 | fs.stat(path, (err) => { 18 | if(!err){ 19 | // Falta o content-type 20 | fs 21 | .createReadStream(path) 22 | .pipe(res) 23 | } 24 | }) 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /aula19-data-project-gutenberg/databases/rdf-to-bulk.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const dir = require('node-dir') 4 | const parseRDF = require('./lib/parse-rdf.js') 5 | const dirname = process.argv[2] 6 | const options = { 7 | match: /\.rdf$/, // Match file names that in '.rdf'. 8 | exclude: ['pg0.rdf'], // Ignore the template RDF file (ID = 0). 9 | } 10 | dir.readFiles(dirname, options, (err, content, next) => { 11 | if (err) throw err 12 | const doc = parseRDF(content) 13 | console.log(JSON.stringify({ index: { _id: `pg${doc.id}` } })) 14 | console.log(JSON.stringify(doc)) 15 | next() 16 | }) 17 | 18 | process.stdout.on('error', err => { 19 | if (err.code === 'EPIPE') { 20 | process.exit() 21 | } 22 | throw err // Or take any other appropriate action. 23 | }) -------------------------------------------------------------------------------- /aula35-b4-static-files/lib/static-files.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | module.exports = staticFiles 6 | 7 | /** 8 | * 9 | * @param {*} location The path where are stored the static files. 10 | */ 11 | function staticFiles(location) { 12 | return (req, res, next) => { 13 | if(req.path.indexOf('.') < 0) // If not requesting a static file 14 | return next() 15 | // Read a file from disk and send it to the response 16 | const path = location + req.path 17 | fs.stat(path, (err) => { 18 | if(!err){ 19 | // Falta o content-type 20 | fs 21 | .createReadStream(path) 22 | .pipe(res) 23 | } 24 | }) 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /aula41-cookies-session/app02-cookies-parser.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const express = require('express') 4 | const cookieParser = require('cookie-parser') 5 | /** 6 | * Bag 7 | */ 8 | const bag = { 9 | items: ['iphone', 'laptop', 'bike'], 10 | total: 1750 11 | } 12 | /** 13 | * Setup express 14 | */ 15 | const app = express() 16 | 17 | app.use(cookieParser()) 18 | app.use((req, resp, next) => { 19 | console.log(req.cookies) 20 | next() 21 | }) 22 | app.use((req, resp) => { 23 | resp.cookie('username', 'fmcarvalho', {expires: new Date(Date.now() - 1000)}) 24 | resp.cookie('bag', JSON.stringify(bag)) 25 | resp.cookie('location', 'Lisbon') 26 | resp.end() 27 | }) 28 | /** 29 | * Launch server 30 | */ 31 | app.listen(3000) 32 | console.log('Listening on port 3000...') -------------------------------------------------------------------------------- /aula42-b4-authentication/lib/static-files.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | module.exports = staticFiles 6 | 7 | /** 8 | * 9 | * @param {*} location The path where are stored the static files. 10 | */ 11 | function staticFiles(location) { 12 | return (req, res, next) => { 13 | if(req.path.indexOf('.') < 0) // If not requesting a static file 14 | return next() 15 | // Read a file from disk and send it to the response 16 | const path = location + req.path 17 | fs.stat(path, (err) => { 18 | if(!err){ 19 | // Falta o content-type 20 | fs 21 | .createReadStream(path) 22 | .pipe(res) 23 | } 24 | }) 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /aula36-b4-front-end-javascript/lib/static-files.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | module.exports = staticFiles 6 | 7 | /** 8 | * 9 | * @param {*} location The path where are stored the static files. 10 | */ 11 | function staticFiles(location) { 12 | return (req, res, next) => { 13 | if(req.path.indexOf('.') < 0) // If not requesting a static file 14 | return next() 15 | // Read a file from disk and send it to the response 16 | const path = location + req.path 17 | fs.stat(path, (err) => { 18 | if(!err){ 19 | // Falta o content-type 20 | fs 21 | .createReadStream(path) 22 | .pipe(res) 23 | } 24 | }) 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /aula37-b4-front-end-templates/lib/static-files.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | module.exports = staticFiles 6 | 7 | /** 8 | * 9 | * @param {*} location The path where are stored the static files. 10 | */ 11 | function staticFiles(location) { 12 | return (req, res, next) => { 13 | if(req.path.indexOf('.') < 0) // If not requesting a static file 14 | return next() 15 | // Read a file from disk and send it to the response 16 | const path = location + req.path 17 | fs.stat(path, (err) => { 18 | if(!err){ 19 | // Falta o content-type 20 | fs 21 | .createReadStream(path) 22 | .pipe(res) 23 | } 24 | }) 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/lib/static-files.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | module.exports = staticFiles 6 | 7 | /** 8 | * 9 | * @param {*} location The path where are stored the static files. 10 | */ 11 | function staticFiles(location) { 12 | return (req, res, next) => { 13 | if(req.path.indexOf('.') < 0) // If not requesting a static file 14 | return next() 15 | // Read a file from disk and send it to the response 16 | const path = location + req.path 17 | fs.stat(path, (err) => { 18 | if(!err){ 19 | // Falta o content-type 20 | fs 21 | .createReadStream(path) 22 | .pipe(res) 23 | } 24 | }) 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/lib/static-files.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | 5 | module.exports = staticFiles 6 | 7 | /** 8 | * 9 | * @param {*} location The path where are stored the static files. 10 | */ 11 | function staticFiles(location) { 12 | return (req, res, next) => { 13 | if(req.path.indexOf('.') < 0) // If not requesting a static file 14 | return next() 15 | // Read a file from disk and send it to the response 16 | const path = location + req.path 17 | fs.stat(path, (err) => { 18 | if(!err){ 19 | // Falta o content-type 20 | fs 21 | .createReadStream(path) 22 | .pipe(res) 23 | } 24 | }) 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /aula07-prototype/ex01-prototype.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function Person(fullname, address) { 4 | this.fullname = fullname 5 | this.address = address 6 | } 7 | 8 | Person.prototype.print = function() { 9 | console.log(this.fullname + ': ' + this.address) 10 | } 11 | 12 | 13 | const ze = new Person('Ze', 'Rua das Flores') 14 | const maria = new Person('Maria', 'Rua da Bolivia') 15 | 16 | ze.print() 17 | maria.print() 18 | 19 | 20 | maria.hello = function() { console.log('HEllo da Maria !!!')} 21 | 22 | // <=> Person.prototype.hello = function() {console.log('Hello!!!')} 23 | ze.constructor.prototype.hello = function() {console.log('Hello!!!')} 24 | // !!!! ERRADO: ze.constructor.hello = function() {console.log('Hello!!!')} 25 | 26 | ze.hello() 27 | maria.hello() 28 | 29 | ze.hello() 30 | maria.hello() 31 | -------------------------------------------------------------------------------- /aula41-cookies-session/app03-session.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const express = require('express') 4 | const session = require('express-session') 5 | /** 6 | * Bag 7 | */ 8 | const bag = { 9 | items: ['iphone', 'laptop', 'bike'], 10 | total: 1750 11 | } 12 | /** 13 | * Setup express 14 | */ 15 | const app = express() 16 | app.use(session({secret: 'keyboard cat', resave: false, saveUninitialized: true })) 17 | 18 | app.use((req, resp, next) => { 19 | // console.log(req.cookies) 20 | console.log(req.session) 21 | next() 22 | }) 23 | app.use((req, resp) => { 24 | req.session.username = 'fmcarvalho' 25 | req.session.bag = JSON.stringify(bag) 26 | req.session.location = 'Lisbon' 27 | resp.end() 28 | }) 29 | /** 30 | * Launch server 31 | */ 32 | app.listen(3000) 33 | console.log('Listening on port 3000...') -------------------------------------------------------------------------------- /aula43-b4-passport/app/js/util.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Handlebars = require('./../../node_modules/handlebars/dist/handlebars.js') 4 | const alertView = Handlebars.compile(require('./../views/alert.hbs')) 5 | 6 | module.exports = { 7 | showAlert, 8 | fetchJSON 9 | } 10 | 11 | /** 12 | * Show an alert to the user. 13 | */ 14 | function showAlert(message, type = 'danger') { 15 | document 16 | .getElementById('divAlerts') 17 | .insertAdjacentHTML('beforeend', alertView({type, message})) 18 | } 19 | 20 | /** 21 | * Convenience method to fetch and decode JSON. 22 | */ 23 | async function fetchJSON(url) { 24 | const options = {method: 'GET', credentials: 'same-origin'} 25 | const resp = await fetch(url, options) 26 | const body = await resp.json() 27 | if(resp.status != 200) throw body 28 | else return body 29 | } 30 | -------------------------------------------------------------------------------- /aula42-b4-authentication/app/js/util.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Handlebars = require('./../../node_modules/handlebars/dist/handlebars.js') 4 | const alertView = Handlebars.compile(require('./../views/alert.hbs')) 5 | 6 | module.exports = { 7 | showAlert, 8 | fetchJSON 9 | } 10 | 11 | /** 12 | * Show an alert to the user. 13 | */ 14 | function showAlert(message, type = 'danger') { 15 | document 16 | .getElementById('divAlerts') 17 | .insertAdjacentHTML('beforeend', alertView({type, message})) 18 | } 19 | 20 | /** 21 | * Convenience method to fetch and decode JSON. 22 | */ 23 | async function fetchJSON(url) { 24 | const options = {method: 'GET', credentials: 'same-origin'} 25 | const resp = await fetch(url, options) 26 | const body = await resp.json() 27 | if(resp.status != 200) throw body 28 | else return body 29 | } 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | bower_components 33 | 34 | # Optional npm cache directory 35 | .npm 36 | 37 | # Optional REPL history 38 | .node_repl_history 39 | 40 | 41 | # Ignore project gutenberg data 42 | cache 43 | 44 | 45 | # Transpiled webpack sources 46 | dist -------------------------------------------------------------------------------- /aula43-b4-passport/webpack.config.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const HtmlWebpackPlugin = require('html-webpack-plugin') 4 | 5 | module.exports = { 6 | entry: './app/js/entry.js', 7 | mode: 'development', 8 | devtool: 'source-map', 9 | plugins: [ 10 | new HtmlWebpackPlugin({ 11 | favicon: './app/img/Frogatto.png', 12 | title: 'b4' 13 | }) 14 | ], 15 | module: { 16 | rules: [{ 17 | test: /\.css$/, 18 | use: [ 'style-loader', 'css-loader' ] 19 | },{ 20 | test: /\.(jpg|png|woff|woff2|eot|ttf|svg)$/, 21 | loader: 'url-loader?limit=100000', 22 | }, 23 | { 24 | test: /\.hbs$/, 25 | use: 'raw-loader' 26 | }, 27 | { 28 | test: /\.html$/, 29 | use: 'html-loader' 30 | }] 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /aula38-b4-webpack/test/parallel-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | 5 | describe('Test parallel function', () => { 6 | 7 | it('Should execute two tasks with success', done => { 8 | const tasks = [ 9 | () => new Promise((resolve) => 10 | setTimeout(() => resolve('one'), 200)), 11 | () => new Promise((resolve) => 12 | setTimeout(() => resolve('two'), 100)), 13 | ] 14 | const prms = tasks.map(t => t()) 15 | Promise 16 | .all(prms) 17 | .then(results => { 18 | expect(results) 19 | .to.be.an('array') 20 | .with.length(2) 21 | expect(results[0]).to.equal('one') 22 | expect(results[1]).to.equal('two') 23 | done() 24 | }) 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /aula42-b4-authentication/webpack.config.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const HtmlWebpackPlugin = require('html-webpack-plugin') 4 | 5 | module.exports = { 6 | entry: './app/js/entry.js', 7 | mode: 'development', 8 | devtool: 'source-map', 9 | plugins: [ 10 | new HtmlWebpackPlugin({ 11 | favicon: './app/img/Frogatto.png', 12 | title: 'b4' 13 | }) 14 | ], 15 | module: { 16 | rules: [{ 17 | test: /\.css$/, 18 | use: [ 'style-loader', 'css-loader' ] 19 | },{ 20 | test: /\.(jpg|png|woff|woff2|eot|ttf|svg)$/, 21 | loader: 'url-loader?limit=100000', 22 | }, 23 | { 24 | test: /\.hbs$/, 25 | use: 'raw-loader' 26 | }, 27 | { 28 | test: /\.html$/, 29 | use: 'html-loader' 30 | }] 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /aula43-b4-passport/test/parallel-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | 5 | describe('Test parallel function', () => { 6 | 7 | it('Should execute two tasks with success', done => { 8 | const tasks = [ 9 | () => new Promise((resolve) => 10 | setTimeout(() => resolve('one'), 200)), 11 | () => new Promise((resolve) => 12 | setTimeout(() => resolve('two'), 100)), 13 | ] 14 | const prms = tasks.map(t => t()) 15 | Promise 16 | .all(prms) 17 | .then(results => { 18 | expect(results) 19 | .to.be.an('array') 20 | .with.length(2) 21 | expect(results[0]).to.equal('one') 22 | expect(results[1]).to.equal('two') 23 | done() 24 | }) 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /aula32-promises-basics/test/parallel-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | 5 | describe('Test parallel function', () => { 6 | 7 | it('Should execute two tasks with success', done => { 8 | const tasks = [ 9 | () => new Promise((resolve) => 10 | setTimeout(() => resolve('one'), 200)), 11 | () => new Promise((resolve) => 12 | setTimeout(() => resolve('two'), 100)), 13 | ] 14 | const prms = tasks.map(t => t()) 15 | Promise 16 | .all(prms) 17 | .then(results => { 18 | expect(results) 19 | .to.be.an('array') 20 | .with.length(2) 21 | expect(results[0]).to.equal('one') 22 | expect(results[1]).to.equal('two') 23 | done() 24 | }) 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /aula35-b4-static-files/test/parallel-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | 5 | describe('Test parallel function', () => { 6 | 7 | it('Should execute two tasks with success', done => { 8 | const tasks = [ 9 | () => new Promise((resolve) => 10 | setTimeout(() => resolve('one'), 200)), 11 | () => new Promise((resolve) => 12 | setTimeout(() => resolve('two'), 100)), 13 | ] 14 | const prms = tasks.map(t => t()) 15 | Promise 16 | .all(prms) 17 | .then(results => { 18 | expect(results) 19 | .to.be.an('array') 20 | .with.length(2) 21 | expect(results[0]).to.equal('one') 22 | expect(results[1]).to.equal('two') 23 | done() 24 | }) 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/webpack.config.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const HtmlWebpackPlugin = require('html-webpack-plugin') 4 | 5 | module.exports = { 6 | entry: './app/js/entry.js', 7 | mode: 'development', 8 | devtool: 'source-map', 9 | plugins: [ 10 | new HtmlWebpackPlugin({ 11 | favicon: './app/img/Frogatto.png', 12 | title: 'b4' 13 | }) 14 | ], 15 | module: { 16 | rules: [{ 17 | test: /\.css$/, 18 | use: [ 'style-loader', 'css-loader' ] 19 | },{ 20 | test: /\.(jpg|png|woff|woff2|eot|ttf|svg)$/, 21 | loader: 'url-loader?limit=100000', 22 | }, 23 | { 24 | test: /\.hbs$/, 25 | use: 'raw-loader' 26 | }, 27 | { 28 | test: /\.html$/, 29 | use: 'html-loader' 30 | }] 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/webpack.config.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const HtmlWebpackPlugin = require('html-webpack-plugin') 4 | 5 | module.exports = { 6 | entry: './app/js/entry.js', 7 | mode: 'development', 8 | devtool: 'source-map', 9 | plugins: [ 10 | new HtmlWebpackPlugin({ 11 | favicon: './app/img/Frogatto.png', 12 | title: 'b4' 13 | }) 14 | ], 15 | module: { 16 | rules: [{ 17 | test: /\.css$/, 18 | use: [ 'style-loader', 'css-loader' ] 19 | },{ 20 | test: /\.(jpg|png|woff|woff2|eot|ttf|svg)$/, 21 | loader: 'url-loader?limit=100000', 22 | }, 23 | { 24 | test: /\.hbs$/, 25 | use: 'raw-loader' 26 | }, 27 | { 28 | test: /\.html$/, 29 | use: 'html-loader' 30 | }] 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/app/views/bookSearch.html: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 |
5 | 6 |
7 |
8 |
9 | 10 |
11 | 12 |
13 |
14 |
15 |
16 | 17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | -------------------------------------------------------------------------------- /aula42-b4-authentication/test/parallel-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | 5 | describe('Test parallel function', () => { 6 | 7 | it('Should execute two tasks with success', done => { 8 | const tasks = [ 9 | () => new Promise((resolve) => 10 | setTimeout(() => resolve('one'), 200)), 11 | () => new Promise((resolve) => 12 | setTimeout(() => resolve('two'), 100)), 13 | ] 14 | const prms = tasks.map(t => t()) 15 | Promise 16 | .all(prms) 17 | .then(results => { 18 | expect(results) 19 | .to.be.an('array') 20 | .with.length(2) 21 | expect(results[0]).to.equal('one') 22 | expect(results[1]).to.equal('two') 23 | done() 24 | }) 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /aula33-b4-web-server-promises/test/parallel-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | 5 | describe('Test parallel function', () => { 6 | 7 | it('Should execute two tasks with success', done => { 8 | const tasks = [ 9 | () => new Promise((resolve) => 10 | setTimeout(() => resolve('one'), 200)), 11 | () => new Promise((resolve) => 12 | setTimeout(() => resolve('two'), 100)), 13 | ] 14 | const prms = tasks.map(t => t()) 15 | Promise 16 | .all(prms) 17 | .then(results => { 18 | expect(results) 19 | .to.be.an('array') 20 | .with.length(2) 21 | expect(results[0]).to.equal('one') 22 | expect(results[1]).to.equal('two') 23 | done() 24 | }) 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /aula34-html-csss/bookSearch.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | b4app 4 | 5 | 6 | 7 |
8 |

b4app

9 |
10 | 23 |
24 | 25 | -------------------------------------------------------------------------------- /aula36-b4-front-end-javascript/test/parallel-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | 5 | describe('Test parallel function', () => { 6 | 7 | it('Should execute two tasks with success', done => { 8 | const tasks = [ 9 | () => new Promise((resolve) => 10 | setTimeout(() => resolve('one'), 200)), 11 | () => new Promise((resolve) => 12 | setTimeout(() => resolve('two'), 100)), 13 | ] 14 | const prms = tasks.map(t => t()) 15 | Promise 16 | .all(prms) 17 | .then(results => { 18 | expect(results) 19 | .to.be.an('array') 20 | .with.length(2) 21 | expect(results[0]).to.equal('one') 22 | expect(results[1]).to.equal('two') 23 | done() 24 | }) 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /aula37-b4-front-end-templates/test/parallel-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | 5 | describe('Test parallel function', () => { 6 | 7 | it('Should execute two tasks with success', done => { 8 | const tasks = [ 9 | () => new Promise((resolve) => 10 | setTimeout(() => resolve('one'), 200)), 11 | () => new Promise((resolve) => 12 | setTimeout(() => resolve('two'), 100)), 13 | ] 14 | const prms = tasks.map(t => t()) 15 | Promise 16 | .all(prms) 17 | .then(results => { 18 | expect(results) 19 | .to.be.an('array') 20 | .with.length(2) 21 | expect(results[0]).to.equal('one') 22 | expect(results[1]).to.equal('two') 23 | done() 24 | }) 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/test/parallel-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | 5 | describe('Test parallel function', () => { 6 | 7 | it('Should execute two tasks with success', done => { 8 | const tasks = [ 9 | () => new Promise((resolve) => 10 | setTimeout(() => resolve('one'), 200)), 11 | () => new Promise((resolve) => 12 | setTimeout(() => resolve('two'), 100)), 13 | ] 14 | const prms = tasks.map(t => t()) 15 | Promise 16 | .all(prms) 17 | .then(results => { 18 | expect(results) 19 | .to.be.an('array') 20 | .with.length(2) 21 | expect(results[0]).to.equal('one') 22 | expect(results[1]).to.equal('two') 23 | done() 24 | }) 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/test/parallel-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | 5 | describe('Test parallel function', () => { 6 | 7 | it('Should execute two tasks with success', done => { 8 | const tasks = [ 9 | () => new Promise((resolve) => 10 | setTimeout(() => resolve('one'), 200)), 11 | () => new Promise((resolve) => 12 | setTimeout(() => resolve('two'), 100)), 13 | ] 14 | const prms = tasks.map(t => t()) 15 | Promise 16 | .all(prms) 17 | .then(results => { 18 | expect(results) 19 | .to.be.an('array') 20 | .with.length(2) 21 | expect(results[0]).to.equal('one') 22 | expect(results[1]).to.equal('two') 23 | done() 24 | }) 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /aula11-socket-client/app10-tcp-client.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const net = require('net') 4 | 5 | net 6 | .connect({port: 3000}) 7 | .on('data', buffer => { 8 | const msg = JSON.parse(buffer.toString().replace('\n\r', '')) 9 | switch (msg.type) { 10 | case 'watching': 11 | onFileWatching(msg) 12 | break 13 | case 'changed': 14 | onFileChanged(msg) 15 | break 16 | default: 17 | onUnknownMessage(msg) 18 | } 19 | }) 20 | 21 | function onFileWatching(msg) { 22 | console.log(`Now watching: ${msg.file}`) 23 | } 24 | function onFileChanged(msg) { 25 | const date = new Date(msg.timestamp) 26 | console.log(`File ${msg.file} changed: ${date}`) 27 | } 28 | function onUnknownMessage(msg) { 29 | console.log(`Unrecognized message type: ${msg.type}`) 30 | } 31 | -------------------------------------------------------------------------------- /aula12-stream-json-parser/app10-tcp-client.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const net = require('net') 4 | 5 | net 6 | .connect({port: 3000}) 7 | .on('data', buffer => { 8 | const msg = JSON.parse(buffer.toString().replace('\n\r', '')) 9 | switch (msg.type) { 10 | case 'watching': 11 | onFileWatching(msg) 12 | break 13 | case 'changed': 14 | onFileChanged(msg) 15 | break 16 | default: 17 | onUnknownMessage(msg) 18 | } 19 | }) 20 | 21 | function onFileWatching(msg) { 22 | console.log(`Now watching: ${msg.file}`) 23 | } 24 | function onFileChanged(msg) { 25 | const date = new Date(msg.timestamp) 26 | console.log(`File ${msg.file} changed: ${date}`) 27 | } 28 | function onUnknownMessage(msg) { 29 | console.log(`Unrecognized message type: ${msg.type}`) 30 | } 31 | -------------------------------------------------------------------------------- /aula38-b4-webpack/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula22-b4-web-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bundles-server.js", 6 | "scripts": { 7 | "start": "webpack && node bundles-server.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "bootstrap": "^4.1.3", 14 | "css-loader": "^2.0.0", 15 | "express": "^4.16.4", 16 | "handlebars": "^4.0.12", 17 | "html-loader": "^0.5.5", 18 | "html-webpack-plugin": "^3.2.0", 19 | "morgan": "^1.9.1", 20 | "raw-loader": "^1.0.0", 21 | "request": "^2.88.0", 22 | "request-promise": "^4.2.2", 23 | "style-loader": "^0.23.1", 24 | "url-loader": "^1.1.2", 25 | "webpack": "^4.27.1", 26 | "webpack-cli": "^3.1.2" 27 | }, 28 | "devDependencies": { 29 | "chai": "^4.2.0", 30 | "mocha": "^5.2.0" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /aula19-data-project-gutenberg/databases/lib/parse-rdf.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const cheerio = require('cheerio') 4 | 5 | module.exports = parseRdf 6 | 7 | function parseRdf(rdf) { 8 | const $ = cheerio.load(rdf) 9 | const book = {} 10 | book.id = // Set the book's id. 11 | + // Unary plus casts the result as a number. 12 | $('pgterms\\:ebook') // Query for the tag. 13 | .attr('rdf:about') // Get the value of the rdf:about attribute. 14 | .replace('ebooks/', '') // Strip off the leading 'ebooks/' substring. 15 | book.title = $('dcterms\\:title').text() 16 | book.authors = $('pgterms\\:agent pgterms\\:name') 17 | .toArray() 18 | .map(elem => $(elem).text()) 19 | book.subjects = $('[rdf\\:resource$="/LCSH"]') 20 | .parent() 21 | .find('rdf\\:value') 22 | .toArray() 23 | .map(elem => $(elem).text()) 24 | return book 25 | } -------------------------------------------------------------------------------- /aula43-b4-passport/app/views/bookSearch.html: -------------------------------------------------------------------------------- 1 |

Search for a book by title or author

2 |
3 |
4 | 5 |
6 | 7 |
8 |
9 |
10 | 11 |
12 | 13 |
14 |
15 |
16 |
17 | 18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | -------------------------------------------------------------------------------- /aula12-stream-json-parser/app11-tcp-client-json-parser.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const net = require('net') 4 | const StreamJsonParser = require('./lib/stream-json-parser') 5 | 6 | StreamJsonParser 7 | .connect(net.connect({port: 3000})) 8 | .on('message', msg => { 9 | switch (msg.type) { 10 | case 'watching': 11 | onFileWatching(msg) 12 | break 13 | case 'changed': 14 | onFileChanged(msg) 15 | break 16 | default: 17 | onUnknownMessage(msg) 18 | } 19 | }) 20 | 21 | function onFileWatching(msg) { 22 | console.log(`Now watching: ${msg.file}`) 23 | } 24 | function onFileChanged(msg) { 25 | const date = new Date(msg.timestamp) 26 | console.log(`File ${msg.file} changed: ${date}`) 27 | } 28 | function onUnknownMessage(msg) { 29 | console.log(`Unrecognized message type: ${msg.type}`) 30 | } 31 | -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula22-b4-web-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bundles-server.js", 6 | "scripts": { 7 | "start": "webpack && node bundles-server.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "bootstrap": "^4.1.3", 14 | "css-loader": "^2.0.0", 15 | "express": "^4.16.4", 16 | "handlebars": "^4.0.12", 17 | "html-loader": "^0.5.5", 18 | "html-webpack-plugin": "^3.2.0", 19 | "morgan": "^1.9.1", 20 | "raw-loader": "^1.0.0", 21 | "request": "^2.88.0", 22 | "request-promise": "^4.2.2", 23 | "style-loader": "^0.23.1", 24 | "url-loader": "^1.1.2", 25 | "webpack": "^4.27.1", 26 | "webpack-cli": "^3.1.2" 27 | }, 28 | "devDependencies": { 29 | "chai": "^4.2.0", 30 | "mocha": "^5.2.0" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /aula42-b4-authentication/app/views/bookSearch.html: -------------------------------------------------------------------------------- 1 |

Search for a book by title or author

2 |
3 |
4 | 5 |
6 | 7 |
8 |
9 |
10 | 11 |
12 | 13 |
14 |
15 |
16 |
17 | 18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/app/views/bookSearch.html: -------------------------------------------------------------------------------- 1 |

Search for a book by title or author

2 |
3 |
4 | 5 |
6 | 7 |
8 |
9 |
10 | 11 |
12 | 13 |
14 |
15 |
16 |
17 | 18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | -------------------------------------------------------------------------------- /aula09-java-Async/src/main/java/App.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.nio.file.Files; 3 | import java.nio.file.Paths; 4 | import java.io.IOException; 5 | import org.javaync.io.AsyncFiles; 6 | 7 | public class App { 8 | 9 | public static void main(String[] args) throws Exception { 10 | if(args.length == 0) 11 | throw new RuntimeException("You must provide the name of the files to read!"); 12 | 13 | Arrays 14 | .stream(args) 15 | .forEach(filename -> { 16 | System.out.println("Reading " + filename); 17 | AsyncFiles 18 | .readAll(filename, (err, data) -> { 19 | String lines = new String(data); 20 | System.out.println("##################################"); 21 | System.out.println(lines); 22 | }); 23 | }); 24 | Thread.sleep(2000); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /aula22-b4-web-server/bundles-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const http = require('http') 4 | const url = require('url') 5 | const bundlesWebApi = require('./bundles-web-api') 6 | 7 | const webServer = new WebServer() 8 | bundlesWebApi(webServer) 9 | http 10 | .createServer(webServer.router) 11 | .listen(3000, () => console.log('Server running on port 3000')) 12 | 13 | function WebServer() { 14 | const routes = [] 15 | this.use = (r) => { 16 | routes.push(r) 17 | } 18 | this.router = (req, resp) => { 19 | const {pathname} = url.parse(req.url, true) // true to parse also the query-string 20 | const method = req.method 21 | console.log(`${Date()}: ${method} request to ${pathname}`) 22 | 23 | for (let index = 0; index < routes.length; index++) { 24 | const r = routes[index] 25 | if(r(req, resp)) { 26 | // The route sends a response 27 | break 28 | } 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula22-b4-web-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bundles-server.js", 6 | "scripts": { 7 | "start": "node bundles-server.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "bootstrap": "^4.1.3", 14 | "css-loader": "^2.0.0", 15 | "express": "^4.16.4", 16 | "handlebars": "^4.0.12", 17 | "html-loader": "^0.5.5", 18 | "html-webpack-plugin": "^3.2.0", 19 | "morgan": "^1.9.1", 20 | "nconf": "^0.10.0", 21 | "raw-loader": "^1.0.0", 22 | "request": "^2.88.0", 23 | "request-promise": "^4.2.2", 24 | "style-loader": "^0.23.1", 25 | "url-loader": "^1.1.2", 26 | "webpack": "^4.27.1", 27 | "webpack-cli": "^3.1.2", 28 | "webpack-dev-middleware": "^3.4.0" 29 | }, 30 | "devDependencies": { 31 | "chai": "^4.2.0", 32 | "mocha": "^5.2.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /aula28-b4-web-server-Router/bundles-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const http = require('http') 4 | const url = require('url') 5 | const bundlesWebApi = require('./bundles-web-api') 6 | 7 | const webServer = new WebServer() 8 | bundlesWebApi(webServer) 9 | http 10 | .createServer(webServer.router) 11 | .listen(3000, () => console.log('Server running on port 3000')) 12 | 13 | function WebServer() { 14 | const routes = [] 15 | this.use = (r) => { 16 | routes.push(r) 17 | } 18 | this.router = (req, resp) => { 19 | const {pathname} = url.parse(req.url, true) // true to parse also the query-string 20 | const method = req.method 21 | console.log(`${Date()}: ${method} request to ${pathname}`) 22 | 23 | for (let index = 0; index < routes.length; index++) { 24 | const r = routes[index] 25 | if(r(req, resp)) { 26 | // The route sends a response 27 | break 28 | } 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /aula13-unit-tests/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Mocha Tests", 11 | "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", 12 | "args": [ 13 | "-u", 14 | "tdd", 15 | "--timeout", 16 | "999999", 17 | "--colors", 18 | "${workspaceFolder}/test" 19 | ], 20 | "internalConsoleOptions": "openOnSessionStart" 21 | }, 22 | { 23 | "type": "node", 24 | "request": "launch", 25 | "name": "Launch Program", 26 | "program": "${workspaceFolder}\\index.js" 27 | } 28 | ] 29 | } -------------------------------------------------------------------------------- /aula43-b4-passport/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Mocha Tests", 11 | "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", 12 | "args": [ 13 | "-u", 14 | "tdd", 15 | "--timeout", 16 | "999999", 17 | "--colors", 18 | "${workspaceFolder}/test" 19 | ], 20 | "internalConsoleOptions": "openOnSessionStart" 21 | }, 22 | { 23 | "type": "node", 24 | "request": "launch", 25 | "name": "Launch Program", 26 | "program": "${workspaceFolder}\\bundles-server.js" 27 | } 28 | ] 29 | } -------------------------------------------------------------------------------- /aula09-java-sync/App.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.nio.file.Files; 3 | import java.nio.file.Paths; 4 | import java.io.IOException; 5 | 6 | public class App { 7 | 8 | public static void main(String[] args) { 9 | if(args.length == 0) 10 | throw new RuntimeException("You must provide the name of the files to read!"); 11 | 12 | Arrays 13 | .stream(args) 14 | .forEach(filename -> { 15 | System.out.println("Reading " + filename); 16 | String lines = readAll(filename); 17 | System.out.println("##################################"); 18 | System.out.println(lines); 19 | }); 20 | } 21 | 22 | public static String readAll(String filename) { 23 | try{ 24 | byte[] data = Files.readAllBytes(Paths.get(filename)); 25 | return new String(data); 26 | } catch(IOException e) { 27 | throw new RuntimeException(e); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /aula42-b4-authentication/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Mocha Tests", 11 | "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", 12 | "args": [ 13 | "-u", 14 | "tdd", 15 | "--timeout", 16 | "999999", 17 | "--colors", 18 | "${workspaceFolder}/test" 19 | ], 20 | "internalConsoleOptions": "openOnSessionStart" 21 | }, 22 | { 23 | "type": "node", 24 | "request": "launch", 25 | "name": "Launch Program", 26 | "program": "${workspaceFolder}\\bundles-server.js" 27 | } 28 | ] 29 | } -------------------------------------------------------------------------------- /aula32-promises-basics/app02-delay.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function delay(timeout, success) { 4 | return new Promise((resolve, reject)=> { 5 | setTimeout(() => { 6 | if(success) 7 | resolve(`RESOLVED after ${timeout} ms`) 8 | else 9 | reject(`REJECTED after ${timeout} ms`) 10 | }, timeout) 11 | }) 12 | } 13 | 14 | const prm = delay(2000, true) // Pending 15 | const fail = delay(2000, false) // Pending 16 | // forwardPromise(prm) 17 | asyncForward(fail) 18 | console.log('Begin delays...') 19 | 20 | async function asyncForward(prm) { 21 | try{ 22 | const str = await prm 23 | const nr = str.length 24 | console.log('Number = ' + nr) 25 | } 26 | catch(err) { 27 | console.log(err) 28 | } 29 | } 30 | 31 | 32 | function forwardPromise(prm) { 33 | prm 34 | .then(str => str.length) // Resolved 35 | .then(nr => 'Number = ' + nr) // Resolved 36 | .then(console.log) 37 | .catch(console.log) 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /aula42-b4-authentication/app/views/navbar.hbs: -------------------------------------------------------------------------------- 1 | 30 | 31 | -------------------------------------------------------------------------------- /aula07-prototype/ex02-inheritance.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function Person(fullname, address) { 4 | this.fullname = fullname 5 | this.address = address 6 | } 7 | 8 | Person.prototype.print = function() { 9 | console.log(this.fullname + ': ' + this.address) 10 | } 11 | 12 | function Student(fullname, address, nr) { 13 | Person.call(this, fullname, address) 14 | // <=> Person.apply(this, arguments) // c# Invoke(object target, object[] args) 15 | this.nr = nr 16 | } 17 | 18 | // Student.prototype = Person.prototype // !!! PROBLEMA informação de Person = Student 19 | // <=> Student.prototype = new Person() 20 | Student.prototype = Object.create(Person.prototype) 21 | 22 | Student.prototype.grades = function(){ 23 | console.log('Very Good!') 24 | } 25 | 26 | const ze = new Person('Ze', 'Rua das Flores') 27 | const maria = new Person('Maria', 'Rua da Bolivia') 28 | const tome = new Student('Tome', 'Rua da Argentina', 76154) 29 | 30 | ze.print() 31 | maria.print() 32 | tome.print() 33 | tome.grades() 34 | if('grades' in ze) ze.grades() 35 | else console.log('Ze has no grades!') -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/bundles-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const http = require('http') 4 | const express = require('express') 5 | const morgan = require('morgan') 6 | const bundlesWebApi = require('./bundles-web-api') 7 | const webpackConfig = require('./webpack.config.js') 8 | const webpack = require('webpack') 9 | const webpackMiddleware = require('webpack-dev-middleware') 10 | const nconf = require('nconf') 11 | 12 | nconf 13 | .argv() 14 | .env() 15 | .defaults({'NODE_ENV': 'development'}) 16 | 17 | const webServer = express() 18 | webServer.use(morgan('dev')) 19 | 20 | const NODE_ENV = nconf.get('NODE_ENV') 21 | const isDev = NODE_ENV == 'development' 22 | 23 | console.log('Running ' + NODE_ENV) 24 | 25 | if(isDev) 26 | webServer.use(webpackMiddleware(webpack(webpackConfig), { 27 | publicPath: '/', 28 | stats: {colors: true} 29 | })) 30 | else 31 | webServer.use(express.static('dist')) 32 | 33 | bundlesWebApi(webServer) 34 | http 35 | .createServer(webServer) 36 | .listen(3000, () => console.log('Server running on port 3000')) 37 | -------------------------------------------------------------------------------- /aula43-b4-passport/app/views/navbar.hbs: -------------------------------------------------------------------------------- 1 | 30 | 31 | -------------------------------------------------------------------------------- /aula09-java-Async/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This file was generated by the Gradle 'init' task. 3 | * 4 | * This generated file contains a sample Java project to get you started. 5 | * For more details take a look at the Java Quickstart chapter in the Gradle 6 | * user guide available at https://docs.gradle.org/4.6/userguide/tutorial_java_projects.html 7 | */ 8 | 9 | plugins { 10 | // Apply the java plugin to add support for Java 11 | id 'java' 12 | 13 | // Apply the application plugin to add support for building an application 14 | id 'application' 15 | } 16 | 17 | // Define the main class for the application 18 | mainClassName = 'App' 19 | 20 | dependencies { 21 | // This dependency is found on compile classpath of this component and consumers. 22 | compile 'com.github.javasync:AsyncFileRw:1.1.2' 23 | } 24 | 25 | // In this section you declare where to find the dependencies of your project 26 | repositories { 27 | // Use jcenter for resolving your dependencies. 28 | // You can declare any Maven/Ivy/file repository here. 29 | // jcenter() 30 | mavenCentral() 31 | } 32 | -------------------------------------------------------------------------------- /aula12-stream-json-parser/app10-tcp-Json-Flaw-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | const net = require('net') 5 | 6 | const server = net 7 | .createServer(onSocketSubscription) 8 | .listen(3000, console.log.bind(console, 'Flaw server listening for subscribers...')) 9 | 10 | function onSocketSubscription(socket) { 11 | // Reporting. 12 | console.log('Subscriber connected.') 13 | 14 | // Two message chunks that together make a whole message. 15 | const firstChunk = '{"type":"changed","timesta' 16 | const secondChunk = 'mp":1450694370094}\n\r' 17 | // Send the first chunk immediately. 18 | socket.write(firstChunk) 19 | // After a short delay, send the other chunk. 20 | setTimeout(() => { 21 | socket.write(secondChunk) 22 | socket.end() 23 | }, 100) 24 | 25 | // Cleanup. 26 | socket.on('close', onSocketDisconnection) 27 | socket.on('error', err => console.log('ERROR: ' + err.code)) 28 | } 29 | 30 | function onSocketDisconnection() { 31 | console.log('Subscriber disconnected.') 32 | server.close() 33 | } 34 | 35 | -------------------------------------------------------------------------------- /aula43-b4-passport/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula22-b4-web-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bundles-server.js", 6 | "scripts": { 7 | "start": "node bundles-server.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "body-parser": "^1.18.3", 14 | "bootstrap": "^4.1.3", 15 | "css-loader": "^2.0.0", 16 | "express": "^4.16.4", 17 | "express-session": "^1.15.6", 18 | "handlebars": "^4.0.12", 19 | "html-loader": "^0.5.5", 20 | "html-webpack-plugin": "^3.2.0", 21 | "jquery": "^3.3.1", 22 | "morgan": "^1.9.1", 23 | "nconf": "^0.10.0", 24 | "passport": "^0.4.0", 25 | "popper.js": "^1.14.6", 26 | "raw-loader": "^1.0.0", 27 | "request": "^2.88.0", 28 | "request-promise": "^4.2.2", 29 | "style-loader": "^0.23.1", 30 | "url-loader": "^1.1.2", 31 | "webpack": "^4.27.1", 32 | "webpack-cli": "^3.1.2", 33 | "webpack-dev-middleware": "^3.4.0" 34 | }, 35 | "devDependencies": { 36 | "chai": "^4.2.0", 37 | "mocha": "^5.2.0" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /aula42-b4-authentication/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aula22-b4-web-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "bundles-server.js", 6 | "scripts": { 7 | "start": "node bundles-server.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "body-parser": "^1.18.3", 14 | "bootstrap": "^4.1.3", 15 | "css-loader": "^2.0.0", 16 | "express": "^4.16.4", 17 | "express-session": "^1.15.6", 18 | "handlebars": "^4.0.12", 19 | "html-loader": "^0.5.5", 20 | "html-webpack-plugin": "^3.2.0", 21 | "jquery": "^3.3.1", 22 | "morgan": "^1.9.1", 23 | "nconf": "^0.10.0", 24 | "passport": "^0.4.0", 25 | "popper.js": "^1.14.6", 26 | "raw-loader": "^1.0.0", 27 | "request": "^2.88.0", 28 | "request-promise": "^4.2.2", 29 | "style-loader": "^0.23.1", 30 | "url-loader": "^1.1.2", 31 | "webpack": "^4.27.1", 32 | "webpack-cli": "^3.1.2", 33 | "webpack-dev-middleware": "^3.4.0" 34 | }, 35 | "devDependencies": { 36 | "chai": "^4.2.0", 37 | "mocha": "^5.2.0" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/app/js/entry.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | require('./../../node_modules/bootstrap/dist/css/bootstrap.css') 4 | const bookSearch = require('./bookSearch') 5 | const b4index = require('./b4index') 6 | const mainView = require('./../views/main.html') 7 | 8 | document.body.innerHTML = mainView 9 | const divMain = document.getElementById('divMain') 10 | 11 | window.onhashchange = showView 12 | window.onload = showView 13 | 14 | function showView() { 15 | const path = window.location.hash 16 | switch(path) { 17 | case '#b4index': 18 | b4index(divMain) 19 | break 20 | case '#bookSearch': 21 | bookSearch(divMain) 22 | break 23 | default: 24 | divMain.innerHTML = 'Resource not found!' 25 | } 26 | updateNav(path) 27 | } 28 | function updateNav(path){ 29 | // Deactivate previous anchor 30 | const prev = document.querySelector('a.active') 31 | if(prev) prev.classList.remove('active') 32 | 33 | // Activate anchor in navigation bar 34 | const option = document.getElementById('nav' + path) 35 | if(option) option.classList.add('active') 36 | } -------------------------------------------------------------------------------- /aula19-data-project-gutenberg/databases/test/parse-rdf-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | const expect = require('chai').expect 5 | const rdf = fs.readFileSync(`${__dirname}/pg132.rdf`) 6 | const parseRDF = require('../lib/parse-rdf.js') 7 | 8 | describe('parseRDF', () => { 9 | 10 | it('should be a function', () => { 11 | expect(parseRDF).to.be.a('function') 12 | }) 13 | 14 | it('should parse RDF content', () => { 15 | const book = parseRDF(rdf) 16 | expect(book).to.be.an('object') 17 | .and.to.have.a.property('id', 132) 18 | expect(book).to.have.a.property('title', 'The Art of War') 19 | expect(book) 20 | .to.have.a.property('authors') 21 | .that.is.an('array').with.lengthOf(2) 22 | .and.contains('Sunzi, active 6th century B.C.') 23 | .and.contains('Giles, Lionel') 24 | expect(book) 25 | .to.have.a.property('subjects') 26 | .that.is.an('array').with.lengthOf(2) 27 | .and.contains('Military art and science -- Early works to 1800') 28 | .and.contains('War -- Early works to 1800') 29 | }) 30 | }) 31 | 32 | -------------------------------------------------------------------------------- /aula38-b4-webpack/webpack.config.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const HtmlWebpackPlugin = require('html-webpack-plugin') 4 | 5 | module.exports = { 6 | 'entry': { 7 | index: './app/entry.js' 8 | }, 9 | mode: 'development', 10 | devtool: 'source-map', 11 | plugins: [ 12 | new HtmlWebpackPlugin({ 13 | template: './app/b4index.html', 14 | inject: true, 15 | chunks: ['index'], 16 | filename: 'b4index.html' 17 | }), 18 | new HtmlWebpackPlugin({ 19 | template: './app/bookSearch.html', 20 | inject: true, 21 | chunks: ['index'], 22 | filename: 'bookSearch.html' 23 | }) 24 | ], 25 | module: { 26 | rules: [{ 27 | test: /\.css$/, 28 | use: [ 'style-loader', 'css-loader' ] 29 | },{ 30 | test: /\.(jpg|png|woff|woff2|eot|ttf|svg)$/, 31 | loader: 'url-loader?limit=100000', 32 | }, 33 | { 34 | test: /\.(hbs)$/, 35 | use: 'raw-loader' 36 | }, 37 | { 38 | test: /\.(html)$/, 39 | use: 'html-loader' 40 | }] 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /aula43-b4-passport/app/views/main.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | b4 5 | 6 | 7 |
8 |

b4app

9 |
10 |
11 |
12 |
13 |
14 | 15 |
16 |

17 | 34 |
35 | 36 | -------------------------------------------------------------------------------- /aula42-b4-authentication/app/views/main.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | b4 5 | 6 | 7 |
8 |

b4app

9 |
10 |
11 |
12 |
13 |
14 | 15 |
16 |

17 | 34 |
35 | 36 | -------------------------------------------------------------------------------- /aula38-b4-webpack/app/bookSearch.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Handlebars = require('./../node_modules/handlebars/dist/handlebars.js') 4 | const searchResultsTemplate = require('./bookSearchResults.hbs') 5 | 6 | document 7 | .getElementById('buttonSearch') 8 | .addEventListener('click', searchHandler) 9 | 10 | const inputTitle = document.getElementById('inputTitle') 11 | const inputAuthors = document.getElementById('inputAuthors') 12 | const divSearchResults = document.getElementById('divSearchResults') 13 | const searchResultsView = Handlebars.compile(searchResultsTemplate) 14 | 15 | function searchHandler(ev){ 16 | ev.preventDefault() 17 | const title = inputTitle.value 18 | const authors = inputAuthors.value 19 | fetch(`http://localhost:3000/api/books/search?title=${title}&authors=${authors}`) 20 | .then(res => res.json()) 21 | .then(arr => divSearchResults.innerHTML = searchResults(arr)) 22 | .catch(err => console.log(err)) 23 | } 24 | /** 25 | * Returns an HTML partial view binding this template with a Book object. 26 | * 27 | * @param {*} books Array of Book objects with: title, authors array and subjects array. 28 | */ 29 | function searchResults(books) { 30 | return searchResultsView({books}) 31 | } 32 | 33 | -------------------------------------------------------------------------------- /aula43-b4-passport/app/views/login.html: -------------------------------------------------------------------------------- 1 |

Login

2 |
3 |
4 | 5 |
6 | 7 |
8 |
9 |
10 | 11 |
12 | 13 |
14 |
15 |
16 | 17 |
18 | 19 |
20 |
21 |
22 |
23 | 24 |
25 |
26 | 27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | -------------------------------------------------------------------------------- /aula35-b4-static-files/bundles-web-api.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Bundle = require('./lib/bundle') 4 | // const Bundle = require('./lib/bundle-mock') 5 | 6 | const es = { 7 | host: 'localhost', 8 | port: '9200', 9 | books_index: 'books', 10 | bundles_index: 'bundles' 11 | } 12 | 13 | const bundle = Bundle.init(es) 14 | 15 | module.exports = (app) => { 16 | app.post('/api/bundle', postBundle) 17 | app.get('/api/bundle/:id', getBundle) 18 | app.use(resourceNotFond) 19 | app.use(errorHandler) 20 | 21 | function postBundle(req, resp, next) { 22 | const name = req.query.name 23 | bundle 24 | .create(name) 25 | .then(data => resp.json(data)) 26 | .catch(next) 27 | } 28 | function getBundle(req, resp, next) { 29 | const id = req.params.id 30 | bundle 31 | .get(id) 32 | .then(bundle => resp.json(bundle)) 33 | .catch(next) 34 | } 35 | function resourceNotFond(req, resp, next) { 36 | next({ 37 | 'statusCode': 404, 38 | 'error': 'Resource Not Found!' 39 | }) 40 | } 41 | function errorHandler(err, req, res, next) { 42 | res.statusCode = err.statusCode 43 | res.end(err.error) 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /aula33-b4-web-server-promises/bundles-web-api.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | // const Bundle = require('./lib/bundle') 4 | const Bundle = require('./lib/bundle-mock') 5 | 6 | const es = { 7 | host: 'localhost', 8 | port: '9200', 9 | books_index: 'books', 10 | bundles_index: 'bundles' 11 | } 12 | 13 | const bundle = Bundle.init(es) 14 | 15 | module.exports = (app) => { 16 | app.post('/api/bundle', postBundle) 17 | app.get('/api/bundle/:id', getBundle) 18 | app.use(resourceNotFond) 19 | app.use(errorHandler) 20 | 21 | function postBundle(req, resp, next) { 22 | const name = req.query.name 23 | bundle 24 | .create(name) 25 | .then(data => resp.json(data)) 26 | .catch(next) 27 | } 28 | function getBundle(req, resp, next) { 29 | const id = req.params.id 30 | bundle 31 | .get(id) 32 | .then(bundle => resp.json(bundle)) 33 | .catch(next) 34 | } 35 | function resourceNotFond(req, resp, next) { 36 | next({ 37 | 'statusCode': 404, 38 | 'error': 'Resource Not Found!' 39 | }) 40 | } 41 | function errorHandler(err, req, res, next) { 42 | res.statusCode = err.statusCode 43 | res.end(err.error) 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /aula31-b4-web-server-express/bundles-web-api.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Bundle = require('./lib/bundle') 4 | // const Bundle = require('./lib/bundle-mock') 5 | 6 | const es = { 7 | host: 'localhost', 8 | port: '9200', 9 | books_index: 'books', 10 | bundles_index: 'bundles' 11 | } 12 | 13 | const bundle = Bundle.init(es) 14 | 15 | module.exports = (app) => { 16 | app.post('/api/bundle', postBundle) 17 | app.get('/api/bundle/:id', getBundle) 18 | app.use(resourceNotFond) 19 | app.use(errorHandler) 20 | 21 | function postBundle(req, resp, next) { 22 | const name = req.query.name 23 | bundle.create(name, (err, data) => { 24 | if(err) return next(err) 25 | resp.json(data) 26 | }) 27 | } 28 | function getBundle(req, resp, next) { 29 | const id = req.params.id 30 | bundle.get(id, (err, bundle) => { 31 | if(err) return next(err) 32 | resp.json(bundle) 33 | }) 34 | } 35 | function resourceNotFond(req, resp, next) { 36 | next({ 37 | 'code': 404, 38 | 'error': 'Resource Not Found!' 39 | }) 40 | } 41 | function errorHandler(err, req, res, next) { 42 | res.statusCode = err.code 43 | res.end(err.error) 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /aula42-b4-authentication/bundles-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const http = require('http') 4 | const express = require('express') 5 | const morgan = require('morgan') 6 | const bundlesWebApi = require('./bundles-web-api') 7 | const webpackConfig = require('./webpack.config.js') 8 | const webpack = require('webpack') 9 | const webpackMiddleware = require('webpack-dev-middleware') 10 | const nconf = require('nconf') 11 | const bodyParser = require('body-parser') 12 | 13 | /** 14 | * Read configurations 15 | */ 16 | nconf 17 | .argv() 18 | .env() 19 | .defaults({'NODE_ENV': 'development'}) 20 | const NODE_ENV = nconf.get('NODE_ENV') 21 | const isDev = NODE_ENV == 'development' 22 | console.log('Running ' + NODE_ENV) 23 | /** 24 | * Setup the express instance 25 | */ 26 | const app = express() 27 | app.use(bodyParser.urlencoded({ extended: false })) 28 | app.use(bodyParser.json()) 29 | app.use(morgan('dev')) 30 | app.use(frontEndMiddleware(isDev)) 31 | /** 32 | * Add bundle routes 33 | */ 34 | bundlesWebApi(app) 35 | /** 36 | * Starts the web server 37 | */ 38 | http 39 | .createServer(app) 40 | .listen(3000, () => console.log('Server running on port 3000')) 41 | 42 | function frontEndMiddleware(isDev) { 43 | return isDev 44 | ? webpackMiddleware(webpack(webpackConfig)) 45 | : express.static('dist') 46 | } -------------------------------------------------------------------------------- /aula10-socket/app10-tcp-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | const net = require('net') 5 | 6 | const filename = process.argv[2] 7 | let counter = 0 8 | 9 | if (!filename) { 10 | throw Error('Error: No filename specified.') 11 | } 12 | 13 | const server = net.createServer(onSubscription) 14 | 15 | server.listen(3000) 16 | console.log('Listening for new connections!') 17 | 18 | function onSubscription(socket){ 19 | counter++ // add a client 20 | 21 | // Reporting. 22 | console.log('Subscriber connected.') 23 | const msg = JSON.stringify({type: 'watching', file: filename, timestamp: Date.now()}) 24 | socket.write(msg + '\n\r') 25 | 26 | // Watcher setup. 27 | const watcher = fs.watch(filename, onFileChanges.bind(null, socket)) 28 | 29 | // Cleanup. 30 | socket.on('close', onSocketClose.bind(null, watcher)) 31 | } 32 | 33 | function onFileChanges(socket){ 34 | const obj = { 35 | type: 'changed', 36 | file: filename, 37 | timestamp: Date.now() 38 | } 39 | const msg = JSON.stringify(obj) // Object => string 40 | socket.write(msg + '\n\r') 41 | } 42 | 43 | function onSocketClose(watcher){ 44 | counter-- 45 | console.log('Subscriber disconnected.') 46 | watcher.close() 47 | if(counter == 0){ 48 | server.close() 49 | } 50 | } -------------------------------------------------------------------------------- /aula37-b4-front-end-templates/public/bookSearch.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | window.onload = () => { 4 | document 5 | .getElementById('buttonSearch') 6 | .addEventListener('click', searchHandler) 7 | 8 | const inputTitle = document.getElementById('inputTitle') 9 | const inputAuthors = document.getElementById('inputAuthors') 10 | const divSearchResults = document.getElementById('divSearchResults') 11 | const searchResultsTemplate = document 12 | .getElementById('frameSearchResults') 13 | .contentDocument 14 | .body.innerText 15 | const searchResultsView = Handlebars.compile(searchResultsTemplate) 16 | 17 | function searchHandler(ev){ 18 | ev.preventDefault() 19 | const title = inputTitle.value 20 | const authors = inputAuthors.value 21 | fetch(`http://localhost:3000/api/books/search?title=${title}&authors=${authors}`) 22 | .then(res => res.json()) 23 | .then(arr => divSearchResults.innerHTML = searchResults(arr)) 24 | .catch(err => console.log(err)) 25 | } 26 | /** 27 | * Returns an HTML partial view binding this template with a Book object. 28 | * 29 | * @param {*} books Array of Book objects with: title, authors array and subjects array. 30 | */ 31 | function searchResults(books) { 32 | return searchResultsView({books}) 33 | } 34 | 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /aula03-types/ex04-custom-types.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | 4 | function printProperties(obj) 5 | { 6 | let str = '' 7 | for(let name in obj) { 8 | str += name + ' = ' + obj[name] + ', ' 9 | if(typeof(obj[name]) == 'function') 10 | obj[name]() 11 | } 12 | console.log(str) 13 | } 14 | 15 | function createStudent(name, nr) { 16 | return { 17 | "name": name, 18 | "nr": nr, 19 | "print": function() { 20 | console.log(this.name + ": " + this.nr) 21 | } 22 | } 23 | 24 | } 25 | 26 | teste3() 27 | 28 | function teste3() { 29 | const std3 = function(){ console.log("std3")} 30 | std3.nome = "Katty" 31 | std3.nr = 73164 32 | std3.print= function() { 33 | console.log(this.name + ": " + this.nr) 34 | } 35 | printProperties(std3) 36 | std3() 37 | } 38 | 39 | 40 | function teste2() { 41 | const std1 = createStudent("Ze Manel", 716486) 42 | const std2 = createStudent("maria papoila", 72145375) 43 | std2.address = "Rua das Papoilas" 44 | printProperties(std1) 45 | printProperties(std2) 46 | } 47 | 48 | function teste1() { 49 | const std1 = createStudent("Ze Manel", 716486) 50 | const std2 = createStudent("maria papoila", 72145375) 51 | std2.address = "Rua das Papoilas" 52 | 53 | std2.print() 54 | std2.name = "Nando" 55 | std2.print() 56 | } 57 | 58 | /* 59 | console.log(typeof(std1)) 60 | console.log(typeof(std2)) 61 | console.log(typeof({})) 62 | */ -------------------------------------------------------------------------------- /aula42-b4-authentication/app/js/bookSearch.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Handlebars = require('./../../node_modules/handlebars/dist/handlebars.js') 4 | const searchResultsTemplate = require('./../views/bookSearchResults.hbs') 5 | const bookSearchView = require('./../views/bookSearch.html') 6 | 7 | module.exports = (divMain) => { 8 | 9 | divMain.innerHTML = bookSearchView 10 | document 11 | .getElementById('buttonSearch') 12 | .addEventListener('click', searchHandler) 13 | 14 | const inputTitle = document.getElementById('inputTitle') 15 | const inputAuthors = document.getElementById('inputAuthors') 16 | const divSearchResults = document.getElementById('divSearchResults') 17 | const searchResultsView = Handlebars.compile(searchResultsTemplate) 18 | 19 | function searchHandler(ev){ 20 | ev.preventDefault() 21 | const title = inputTitle.value 22 | const authors = inputAuthors.value 23 | fetch(`http://localhost:3000/api/books/search?title=${title}&authors=${authors}`) 24 | .then(res => res.json()) 25 | .then(arr => divSearchResults.innerHTML = searchResults(arr)) 26 | .catch(err => console.log(err)) 27 | } 28 | /** 29 | * Returns an HTML partial view binding this template with a Book object. 30 | * 31 | * @param {*} books Array of Book objects with: title, authors array and subjects array. 32 | */ 33 | function searchResults(books) { 34 | return searchResultsView({books}) 35 | } 36 | } -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/app/js/bookSearch.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Handlebars = require('./../../node_modules/handlebars/dist/handlebars.js') 4 | const searchResultsTemplate = require('./../views/bookSearchResults.hbs') 5 | const bookSearchView = require('./../views/bookSearch.html') 6 | 7 | module.exports = (divMain) => { 8 | 9 | divMain.innerHTML = bookSearchView 10 | document 11 | .getElementById('buttonSearch') 12 | .addEventListener('click', searchHandler) 13 | 14 | const inputTitle = document.getElementById('inputTitle') 15 | const inputAuthors = document.getElementById('inputAuthors') 16 | const divSearchResults = document.getElementById('divSearchResults') 17 | const searchResultsView = Handlebars.compile(searchResultsTemplate) 18 | 19 | function searchHandler(ev){ 20 | ev.preventDefault() 21 | const title = inputTitle.value 22 | const authors = inputAuthors.value 23 | fetch(`http://localhost:3000/api/books/search?title=${title}&authors=${authors}`) 24 | .then(res => res.json()) 25 | .then(arr => divSearchResults.innerHTML = searchResults(arr)) 26 | .catch(err => console.log(err)) 27 | } 28 | /** 29 | * Returns an HTML partial view binding this template with a Book object. 30 | * 31 | * @param {*} books Array of Book objects with: title, authors array and subjects array. 32 | */ 33 | function searchResults(books) { 34 | return searchResultsView({books}) 35 | } 36 | } -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/app/js/bookSearch.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Handlebars = require('./../../node_modules/handlebars/dist/handlebars.js') 4 | const searchResultsTemplate = require('./../views/bookSearchResults.hbs') 5 | const bookSearchView = require('./../views/bookSearch.html') 6 | 7 | module.exports = (divMain) => { 8 | 9 | divMain.innerHTML = bookSearchView 10 | document 11 | .getElementById('buttonSearch') 12 | .addEventListener('click', searchHandler) 13 | 14 | const inputTitle = document.getElementById('inputTitle') 15 | const inputAuthors = document.getElementById('inputAuthors') 16 | const divSearchResults = document.getElementById('divSearchResults') 17 | const searchResultsView = Handlebars.compile(searchResultsTemplate) 18 | 19 | function searchHandler(ev){ 20 | ev.preventDefault() 21 | const title = inputTitle.value 22 | const authors = inputAuthors.value 23 | fetch(`http://localhost:3000/api/books/search?title=${title}&authors=${authors}`) 24 | .then(res => res.json()) 25 | .then(arr => divSearchResults.innerHTML = searchResults(arr)) 26 | .catch(err => console.log(err)) 27 | } 28 | /** 29 | * Returns an HTML partial view binding this template with a Book object. 30 | * 31 | * @param {*} books Array of Book objects with: title, authors array and subjects array. 32 | */ 33 | function searchResults(books) { 34 | return searchResultsView({books}) 35 | } 36 | } -------------------------------------------------------------------------------- /aula20-b4-Better-Book-Bundle-Builder/test/bundle-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | const should = require('chai').should() 5 | const Bundle = require('./../lib/bundle') 6 | 7 | describe('Test Bundle API', () => { 8 | 9 | const es = { 10 | host: 'localhost', 11 | port: '9200', 12 | books_index: 'books', 13 | bundles_index: 'bundles' 14 | } 15 | 16 | it('Should initialize a Bundle service object', done => { 17 | const bundle = Bundle.init(es) 18 | expect(bundle) 19 | .to.be.an('object') 20 | .and.have.a.property('booksUrl', 'http://localhost:9200/books/book') 21 | expect(bundle) 22 | .have.a.property('bundlesUrl', 'http://localhost:9200/bundles/bundle') 23 | done() 24 | }) 25 | 26 | it('Should create a new bundle', done => { 27 | const bundle = Bundle.init(es) 28 | bundle.create('foo', (err, resp) => { 29 | should.not.exist(err) 30 | should.exist(resp) 31 | expect(resp) 32 | .to.be.an('object') 33 | .and.have.a.property('_id') 34 | bundle.get(resp._id, (err, b) => { 35 | expect(b) 36 | .to.be.an('object') 37 | .and.have.a.property('name', 'foo') 38 | bundle.delete(resp._id, (err) =>{ 39 | should.not.exist(err) 40 | done() 41 | }) 42 | }) 43 | }) 44 | 45 | }) 46 | 47 | 48 | }) -------------------------------------------------------------------------------- /aula43-b4-passport/bundles-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const http = require('http') 4 | const express = require('express') 5 | const morgan = require('morgan') 6 | const bundlesWebApi = require('./bundles-web-api') 7 | const authWebApi = require('./auth-web-api') 8 | const webpackConfig = require('./webpack.config.js') 9 | const webpack = require('webpack') 10 | const webpackMiddleware = require('webpack-dev-middleware') 11 | const nconf = require('nconf') 12 | const bodyParser = require('body-parser') 13 | const expressSession = require('express-session') 14 | 15 | /** 16 | * Read configurations 17 | */ 18 | nconf 19 | .argv() 20 | .env() 21 | .defaults({'NODE_ENV': 'development'}) 22 | const NODE_ENV = nconf.get('NODE_ENV') 23 | const isDev = NODE_ENV == 'development' 24 | console.log('Running ' + NODE_ENV) 25 | /** 26 | * Setup the express instance 27 | */ 28 | const app = express() 29 | app.use(bodyParser.urlencoded({ extended: false })) 30 | app.use(bodyParser.json()) 31 | app.use(morgan('dev')) 32 | app.use(expressSession({secret: 'keyboard cat', resave: false, saveUninitialized: true })) 33 | app.use(frontEndMiddleware(isDev)) 34 | /** 35 | * Add bundle and auth routes 36 | */ 37 | authWebApi(app) 38 | bundlesWebApi(app) 39 | /** 40 | * Starts the web server 41 | */ 42 | http 43 | .createServer(app) 44 | .listen(3000, () => console.log('Server running on port 3000')) 45 | 46 | function frontEndMiddleware(isDev) { 47 | return isDev 48 | ? webpackMiddleware(webpack(webpackConfig)) 49 | : express.static('dist') 50 | } -------------------------------------------------------------------------------- /aula43-b4-passport/app/js/bookSearch.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Handlebars = require('./../../node_modules/handlebars/dist/handlebars.js') 4 | const searchResultsTemplate = require('./../views/bookSearchResults.hbs') 5 | const bookSearchView = require('./../views/bookSearch.html') 6 | const util = require('./util') 7 | 8 | module.exports = (divMain) => { 9 | 10 | divMain.innerHTML = bookSearchView 11 | document 12 | .getElementById('buttonSearch') 13 | .addEventListener('click', searchHandler) 14 | 15 | const inputTitle = document.getElementById('inputTitle') 16 | const inputAuthors = document.getElementById('inputAuthors') 17 | const divSearchResults = document.getElementById('divSearchResults') 18 | const searchResultsView = Handlebars.compile(searchResultsTemplate) 19 | 20 | function searchHandler(ev){ 21 | ev.preventDefault() 22 | const title = inputTitle.value 23 | const authors = inputAuthors.value 24 | util.fetchJSON(`http://localhost:3000/api/books/search?title=${title}&authors=${authors}`) 25 | .then(arr => divSearchResults.innerHTML = searchResults(arr)) 26 | .catch(err => util.showAlert('fetch /api/books/search: ' + JSON.stringify(err))) 27 | } 28 | /** 29 | * Returns an HTML partial view binding this template with a Book object. 30 | * 31 | * @param {*} books Array of Book objects with: title, authors array and subjects array. 32 | */ 33 | function searchResults(books) { 34 | return searchResultsView({books}) 35 | } 36 | } -------------------------------------------------------------------------------- /aula13-unit-tests/lib/stream-json-parser.js: -------------------------------------------------------------------------------- 1 | const EventEmitter = require('events').EventEmitter 2 | 3 | class StreamJsonParser extends EventEmitter { 4 | constructor(socket) { 5 | super() 6 | let buffer = '' 7 | socket.on('data', data => { 8 | buffer += data 9 | let boundary = buffer.indexOf('\n\r') 10 | while (boundary >= 0) { 11 | const input = buffer.substring(0, boundary) 12 | buffer = buffer.substring(boundary + 2) 13 | this.emit('message', JSON.parse(input)) 14 | boundary = buffer.indexOf('\n\r') 15 | } 16 | }) 17 | } 18 | static connect(socket) { 19 | return new StreamJsonParser(socket) 20 | } 21 | } 22 | 23 | module.exports = StreamJsonParser 24 | 25 | // function StreamJsonParser(socket) { 26 | // EventEmitter.call(this) // call super constructor 27 | // let buffer = '' 28 | // socket.on('data', data => { 29 | // buffer += data 30 | // let boundary = buffer.indexOf('\n\r') 31 | // while (boundary >= 0) { 32 | // const input = buffer.substring(0, boundary) 33 | // buffer = buffer.substring(boundary + 2) 34 | // this.emit('message', JSON.parse(input)) 35 | // boundary = buffer.indexOf('\n\r') 36 | // } 37 | // }) 38 | 39 | // } 40 | // // Inherits base type methods 41 | // StreamJsonParser.prototype = Object.create(EventEmitter.prototype) 42 | 43 | // StreamJsonParser.connect = function(socket) { 44 | // return new StreamJsonParser(socket) 45 | // } 46 | -------------------------------------------------------------------------------- /aula12-stream-json-parser/lib/stream-json-parser.js: -------------------------------------------------------------------------------- 1 | const EventEmitter = require('events').EventEmitter 2 | 3 | class StreamJsonParser extends EventEmitter { 4 | constructor(socket) { 5 | super() 6 | let buffer = '' 7 | socket.on('data', data => { 8 | buffer += data 9 | let boundary = buffer.indexOf('\n\r') 10 | while (boundary >= 0) { 11 | const input = buffer.substring(0, boundary) 12 | buffer = buffer.substring(boundary + 2) 13 | this.emit('message', JSON.parse(input)) 14 | boundary = buffer.indexOf('\n\r') 15 | } 16 | }) 17 | } 18 | static connect(socket) { 19 | return new StreamJsonParser(socket) 20 | } 21 | } 22 | 23 | module.exports = StreamJsonParser 24 | 25 | // function StreamJsonParser(socket) { 26 | // EventEmitter.call(this) // call super constructor 27 | // let buffer = '' 28 | // socket.on('data', data => { 29 | // buffer += data 30 | // let boundary = buffer.indexOf('\n\r') 31 | // while (boundary >= 0) { 32 | // const input = buffer.substring(0, boundary) 33 | // buffer = buffer.substring(boundary + 2) 34 | // this.emit('message', JSON.parse(input)) 35 | // boundary = buffer.indexOf('\n\r') 36 | // } 37 | // }) 38 | 39 | // } 40 | // // Inherits base type methods 41 | // StreamJsonParser.prototype = Object.create(EventEmitter.prototype) 42 | 43 | // StreamJsonParser.connect = function(socket) { 44 | // return new StreamJsonParser(socket) 45 | // } 46 | -------------------------------------------------------------------------------- /aula38-b4-webpack/bundles-web-api.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Bundle = require('./lib/bundle') 4 | // const Bundle = require('./lib/bundle-mock') 5 | 6 | const es = { 7 | host: 'localhost', 8 | port: '9200', 9 | books_index: 'books', 10 | bundles_index: 'bundles' 11 | } 12 | 13 | const bundle = Bundle.init(es) 14 | 15 | module.exports = (app) => { 16 | app.post('/api/bundle', postBundle) 17 | app.get('/api/bundle/:id', getBundle) 18 | app.get('/api/books/search', bookSearch) 19 | app.use(resourceNotFond) 20 | app.use(errorHandler) 21 | 22 | function bookSearch(req, resp, next) { 23 | const title = req.query.title 24 | const authors = req.query.authors 25 | bundle 26 | .searchBook(title, authors) 27 | .then(data => resp.json(data)) 28 | .catch(next) 29 | } 30 | 31 | function postBundle(req, resp, next) { 32 | const name = req.query.name 33 | bundle 34 | .create(name) 35 | .then(data => resp.json(data)) 36 | .catch(next) 37 | } 38 | function getBundle(req, resp, next) { 39 | const id = req.params.id 40 | bundle 41 | .get(id) 42 | .then(bundle => resp.json(bundle)) 43 | .catch(next) 44 | } 45 | function resourceNotFond(req, resp, next) { 46 | next({ 47 | 'statusCode': 404, 48 | 'error': 'Resource Not Found!' 49 | }) 50 | } 51 | function errorHandler(err, req, res, next) { 52 | res.statusCode = err.statusCode 53 | res.end(err.error) 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /aula36-b4-front-end-javascript/bundles-web-api.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Bundle = require('./lib/bundle') 4 | // const Bundle = require('./lib/bundle-mock') 5 | 6 | const es = { 7 | host: 'localhost', 8 | port: '9200', 9 | books_index: 'books', 10 | bundles_index: 'bundles' 11 | } 12 | 13 | const bundle = Bundle.init(es) 14 | 15 | module.exports = (app) => { 16 | app.post('/api/bundle', postBundle) 17 | app.get('/api/bundle/:id', getBundle) 18 | app.get('/api/books/search', bookSearch) 19 | app.use(resourceNotFond) 20 | app.use(errorHandler) 21 | 22 | function bookSearch(req, resp, next) { 23 | const title = req.query.title 24 | const authors = req.query.authors 25 | bundle 26 | .searchBook(title, authors) 27 | .then(data => resp.json(data)) 28 | .catch(next) 29 | } 30 | 31 | function postBundle(req, resp, next) { 32 | const name = req.query.name 33 | bundle 34 | .create(name) 35 | .then(data => resp.json(data)) 36 | .catch(next) 37 | } 38 | function getBundle(req, resp, next) { 39 | const id = req.params.id 40 | bundle 41 | .get(id) 42 | .then(bundle => resp.json(bundle)) 43 | .catch(next) 44 | } 45 | function resourceNotFond(req, resp, next) { 46 | next({ 47 | 'statusCode': 404, 48 | 'error': 'Resource Not Found!' 49 | }) 50 | } 51 | function errorHandler(err, req, res, next) { 52 | res.statusCode = err.statusCode 53 | res.end(err.error) 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /aula37-b4-front-end-templates/bundles-web-api.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Bundle = require('./lib/bundle') 4 | // const Bundle = require('./lib/bundle-mock') 5 | 6 | const es = { 7 | host: 'localhost', 8 | port: '9200', 9 | books_index: 'books', 10 | bundles_index: 'bundles' 11 | } 12 | 13 | const bundle = Bundle.init(es) 14 | 15 | module.exports = (app) => { 16 | app.post('/api/bundle', postBundle) 17 | app.get('/api/bundle/:id', getBundle) 18 | app.get('/api/books/search', bookSearch) 19 | app.use(resourceNotFond) 20 | app.use(errorHandler) 21 | 22 | function bookSearch(req, resp, next) { 23 | const title = req.query.title 24 | const authors = req.query.authors 25 | bundle 26 | .searchBook(title, authors) 27 | .then(data => resp.json(data)) 28 | .catch(next) 29 | } 30 | 31 | function postBundle(req, resp, next) { 32 | const name = req.query.name 33 | bundle 34 | .create(name) 35 | .then(data => resp.json(data)) 36 | .catch(next) 37 | } 38 | function getBundle(req, resp, next) { 39 | const id = req.params.id 40 | bundle 41 | .get(id) 42 | .then(bundle => resp.json(bundle)) 43 | .catch(next) 44 | } 45 | function resourceNotFond(req, resp, next) { 46 | next({ 47 | 'statusCode': 404, 48 | 'error': 'Resource Not Found!' 49 | }) 50 | } 51 | function errorHandler(err, req, res, next) { 52 | res.statusCode = err.statusCode 53 | res.end(err.error) 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/bundles-web-api.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Bundle = require('./lib/bundle') 4 | // const Bundle = require('./lib/bundle-mock') 5 | 6 | const es = { 7 | host: 'localhost', 8 | port: '9200', 9 | books_index: 'books', 10 | bundles_index: 'bundles' 11 | } 12 | 13 | const bundle = Bundle.init(es) 14 | 15 | module.exports = (app) => { 16 | app.post('/api/bundle', postBundle) 17 | app.get('/api/bundle/:id', getBundle) 18 | app.get('/api/books/search', bookSearch) 19 | app.use(resourceNotFond) 20 | app.use(errorHandler) 21 | 22 | function bookSearch(req, resp, next) { 23 | const title = req.query.title 24 | const authors = req.query.authors 25 | bundle 26 | .searchBook(title, authors) 27 | .then(data => resp.json(data)) 28 | .catch(next) 29 | } 30 | 31 | function postBundle(req, resp, next) { 32 | const name = req.query.name 33 | bundle 34 | .create(name) 35 | .then(data => resp.json(data)) 36 | .catch(next) 37 | } 38 | function getBundle(req, resp, next) { 39 | const id = req.params.id 40 | bundle 41 | .get(id) 42 | .then(bundle => resp.json(bundle)) 43 | .catch(next) 44 | } 45 | function resourceNotFond(req, resp, next) { 46 | next({ 47 | 'statusCode': 404, 48 | 'error': 'Resource Not Found!' 49 | }) 50 | } 51 | function errorHandler(err, req, res, next) { 52 | res.statusCode = err.statusCode 53 | res.end(err.error) 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/bundles-web-api.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Bundle = require('./lib/bundle') 4 | // const Bundle = require('./lib/bundle-mock') 5 | 6 | const es = { 7 | host: 'localhost', 8 | port: '9200', 9 | books_index: 'books', 10 | bundles_index: 'bundles' 11 | } 12 | 13 | const bundle = Bundle.init(es) 14 | 15 | module.exports = (app) => { 16 | app.post('/api/bundle', postBundle) 17 | app.get('/api/bundle/:id', getBundle) 18 | app.get('/api/books/search', bookSearch) 19 | app.use(resourceNotFond) 20 | app.use(errorHandler) 21 | 22 | function bookSearch(req, resp, next) { 23 | const title = req.query.title 24 | const authors = req.query.authors 25 | bundle 26 | .searchBook(title, authors) 27 | .then(data => resp.json(data)) 28 | .catch(next) 29 | } 30 | 31 | function postBundle(req, resp, next) { 32 | const name = req.query.name 33 | bundle 34 | .create(name) 35 | .then(data => resp.json(data)) 36 | .catch(next) 37 | } 38 | function getBundle(req, resp, next) { 39 | const id = req.params.id 40 | bundle 41 | .get(id) 42 | .then(bundle => resp.json(bundle)) 43 | .catch(next) 44 | } 45 | function resourceNotFond(req, resp, next) { 46 | next({ 47 | 'statusCode': 404, 48 | 'error': 'Resource Not Found!' 49 | }) 50 | } 51 | function errorHandler(err, req, res, next) { 52 | res.statusCode = err.statusCode 53 | res.end(err.error) 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /aula35-b4-static-files/public/bookSearch.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | b4 5 | 6 | 7 | 8 |
9 |

b4app

10 | 18 |
19 |

20 |
21 |
22 | 23 |
24 | 25 |
26 |
27 |
28 | 29 |
30 | 31 |
32 |
33 |
34 |
35 | 36 |
37 |
38 |
39 |
40 | 41 | -------------------------------------------------------------------------------- /aula42-b4-authentication/app/js/bundles.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Handlebars = require('./../../node_modules/handlebars/dist/handlebars.js') 4 | const util = require('./util.js') 5 | const bundlesCreateView = Handlebars.compile(require('./../views/bundlesCreate.hbs')) 6 | const bundlesListView = Handlebars.compile(require('./../views/bundlesList.hbs')) 7 | 8 | module.exports = (divMain) => { 9 | divMain.innerHTML = bundlesCreateView() 10 | const txtBundleName = document.getElementById('txtBundleName') 11 | document 12 | .getElementById('btNewBundle') 13 | .addEventListener('click', newBundleHandler) 14 | 15 | getBundlesView() 16 | .then(view => divMain.insertAdjacentHTML('beforeend', view)) 17 | .catch(err => util.showAlert(err, 'danger')) 18 | 19 | 20 | function newBundleHandler(ev) { 21 | ev.preventDefault() 22 | const url = 'http://localhost:3000/api/bundle' 23 | const options = { 24 | method: 'POST', 25 | body: `name=${txtBundleName.value}`, 26 | headers: { 27 | 'Content-Type': 'application/x-www-form-urlencoded' 28 | } 29 | } 30 | fetch(url, options) 31 | .then(getBundlesView) 32 | .then(view => { 33 | divMain.querySelector('table').remove() 34 | divMain.insertAdjacentHTML('beforeend', view) 35 | }) 36 | .catch(err => util.showAlert(err, 'danger')) 37 | } 38 | function getBundlesView() { 39 | return fetch('http://localhost:3000/api/bundle') 40 | .then(resp => resp.json()) 41 | .then(arr => bundlesListView({'bundles': arr})) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /aula12-stream-json-parser/app10-tcp-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | const net = require('net') 5 | 6 | let counter = 0 7 | 8 | if (process.argv.length == 2) { 9 | throw Error('Error: No filename specified.') 10 | } 11 | 12 | const server = net 13 | .createServer(onSubscription) 14 | .listen(3000, () => console.log('Listening for new connections!')) 15 | 16 | function onSubscription(socket){ 17 | counter++ // add a client 18 | 19 | // Reporting. 20 | console.log('Subscriber connected.') 21 | const files = process.argv.slice(2) 22 | files.forEach(filename => sendMsgWatching(socket, filename)) 23 | 24 | // Watcher setup. 25 | const watchers = files 26 | .map(filename => fs.watch(filename, onFileChanges.bind(null, socket, filename))) 27 | 28 | // Cleanup. 29 | socket.on('close', onSocketClose.bind(null, watchers)) 30 | 31 | socket.on('error', err => console.log('Error on client: ' + err.code)) 32 | } 33 | 34 | function sendMsgWatching(socket, filename) { 35 | const msg = JSON.stringify({ 36 | type: 'watching', 37 | file: filename, 38 | timestamp: Date.now() 39 | }) 40 | socket.write(msg + '\n\r') 41 | } 42 | 43 | function onFileChanges(socket, filename){ 44 | const obj = { 45 | type: 'changed', 46 | file: filename, 47 | timestamp: Date.now() 48 | } 49 | const msg = JSON.stringify(obj) // Object => string 50 | socket.write(msg + '\n\r') 51 | } 52 | 53 | function onSocketClose(watchers){ 54 | counter-- 55 | console.log('Subscriber disconnected.') 56 | watchers.forEach(w => w.close()) 57 | if(counter == 0){ 58 | server.close() 59 | } 60 | } -------------------------------------------------------------------------------- /aula38-b4-webpack/app/b4index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | b4 5 | 6 | 7 |
8 |

b4app

9 | 26 |

Better Book Bundle Builder

27 |

28 | A catalog of 57,000 books from Project Gutenberg. 29 | Moreover, it allows to create groups of books, named bundles, 30 | that aggregate a set of books. 31 |

32 |

References

33 | 45 |
46 | 47 | -------------------------------------------------------------------------------- /aula36-b4-front-end-javascript/public/bookSearch.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | b4 5 | 6 | 7 | 8 | 9 |
10 |

b4app

11 | 19 |
20 |

21 |
22 |
23 | 24 |
25 | 26 |
27 |
28 |
29 | 30 |
31 | 32 |
33 |
34 |
35 |
36 | 37 |
38 |
39 |
40 |
41 | 42 | -------------------------------------------------------------------------------- /aula11-socket-client/app10-tcp-server.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | const net = require('net') 5 | 6 | let counter = 0 7 | 8 | if (process.argv.length == 2) { 9 | throw Error('Error: No filename specified.') 10 | } 11 | 12 | const server = net 13 | .createServer(onSubscription) 14 | .listen(3000, () => console.log('Listening for new connections!')) 15 | 16 | function onSubscription(socket){ 17 | counter++ // add a client 18 | 19 | // Reporting. 20 | console.log('Subscriber connected.') 21 | const files = process.argv.slice(2) 22 | let timeout = 0 23 | files.forEach(filename => { 24 | setTimeout(() => sendMsgWatching(socket, filename), timeout) 25 | timeout += 100 26 | }) 27 | 28 | // Watcher setup. 29 | const watchers = files 30 | .map(filename => fs.watch(filename, onFileChanges.bind(null, socket, filename))) 31 | 32 | // Cleanup. 33 | socket.on('close', onSocketClose.bind(null, watchers)) 34 | 35 | socket.on('error', err => console.log('Error on client: ' + err.code)) 36 | } 37 | 38 | function sendMsgWatching(socket, filename) { 39 | const msg = JSON.stringify({ 40 | type: 'watching', 41 | file: filename, 42 | timestamp: Date.now() 43 | }) 44 | socket.write(msg + '\n\r') 45 | } 46 | 47 | function onFileChanges(socket, filename){ 48 | const obj = { 49 | type: 'changed', 50 | file: filename, 51 | timestamp: Date.now() 52 | } 53 | const msg = JSON.stringify(obj) // Object => string 54 | socket.write(msg + '\n\r') 55 | } 56 | 57 | function onSocketClose(watchers){ 58 | counter-- 59 | console.log('Subscriber disconnected.') 60 | watchers.forEach(w => w.close()) 61 | if(counter == 0){ 62 | server.close() 63 | } 64 | } -------------------------------------------------------------------------------- /aula42-b4-authentication/app/js/entry.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | require('./../../node_modules/bootstrap/dist/css/bootstrap.css') 4 | require('./../../node_modules/bootstrap/dist/js/bootstrap.js') 5 | const Handlebars = require('./../../node_modules/handlebars/dist/handlebars.js') 6 | const util = require('./util.js') 7 | const bookSearch = require('./bookSearch') 8 | const b4index = require('./b4index') 9 | const bundles = require('./bundles') 10 | const mainView = require('./../views/main.html') 11 | const navView = Handlebars.compile(require('./../views/navbar.hbs')) 12 | 13 | document.body.innerHTML = mainView 14 | const divMain = document.getElementById('divMain') 15 | const divNavbar = document.getElementById('divNavbar') 16 | 17 | util.fetchJSON('/api/session') 18 | .catch(err => util.showAlert('fetch /api/session: ' + err)) 19 | .then(session => divNavbar.innerHTML = navView(session)) 20 | 21 | 22 | window.onhashchange = showView 23 | window.onload = showView 24 | util.showAlert('b4app is working!', 'info') 25 | 26 | function showView() { 27 | const path = window.location.hash 28 | switch(path) { 29 | case '#b4index': 30 | b4index(divMain) 31 | break 32 | case '#bundles': 33 | bundles(divMain) 34 | break 35 | case '#bookSearch': 36 | bookSearch(divMain) 37 | break 38 | default: 39 | divMain.innerHTML = 'Resource not found!' 40 | } 41 | updateNav(path) 42 | } 43 | function updateNav(path){ 44 | // Deactivate previous anchor 45 | const prev = document.querySelector('a.active') 46 | if(prev) prev.classList.remove('active') 47 | 48 | // Activate anchor in navigation bar 49 | const option = document.getElementById('nav' + path) 50 | if(option) option.classList.add('active') 51 | } 52 | -------------------------------------------------------------------------------- /aula43-b4-passport/lib/auth.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const rp = require('request-promise') 4 | 5 | class Auth { 6 | static init(es) { 7 | return new Auth(es) 8 | } 9 | 10 | /** 11 | * @param {{host: string, port: number}} es 12 | */ 13 | constructor(es){ 14 | this.usersRefresh = `http://${es.host}:${es.port}/users/_refresh` 15 | this.usersUrl = `http://${es.host}:${es.port}/users/user` 16 | } 17 | 18 | async createUser(fullname, username, password) { 19 | const user = { fullname, username, password} 20 | const options = { 21 | 'uri': this.usersUrl, 22 | 'json': true, 23 | 'body': user 24 | } 25 | const resp = await rp.post(options) 26 | await rp.post(this.usersRefresh) 27 | user._id = resp._id 28 | return user 29 | } 30 | 31 | getUser(userId) { 32 | return rp 33 | .get(`${this.usersUrl}/${userId}`) 34 | .then(body => JSON.parse(body)) 35 | .then(obj => {return { 36 | '_id': obj._id, 37 | 'fullname': obj._source.fullname, 38 | 'username': obj._source.username, 39 | }}) 40 | 41 | } 42 | async authenticate(username, password) { 43 | const url = `${this.usersUrl}/_search?q=username:${username}` 44 | const body = await rp.get(url) 45 | const obj = JSON.parse(body) 46 | if(obj.hits.hits.length == 0) 47 | throw {'statusCode': 404, 'err': 'Username not found!' } 48 | const first = obj.hits.hits[0] 49 | if(first._source.password != password) 50 | throw {'statusCode': 401, 'err': 'Wrong credentials!' } 51 | return { 52 | '_id': first._id 53 | } 54 | } 55 | 56 | } 57 | 58 | module.exports = Auth -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/app/views/main.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | b4 5 | 6 | 7 |
8 |

b4app

9 | 26 |
27 |
28 | 29 |
30 |

31 | 48 |
49 | 50 | -------------------------------------------------------------------------------- /aula22-b4-web-server/lib/bundle-mock.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | class Bundle { 4 | 5 | static init() { 6 | return new Bundle() 7 | } 8 | 9 | create(name, cb) { 10 | const id = 127 11 | bundles[id] = { 12 | 'name': name, 13 | 'books': [] 14 | } 15 | cb(null, {'_id': id }) 16 | } 17 | get(id, cb) { 18 | const bundle = bundles[id] 19 | if(!bundle) { 20 | cb({code: 404}) 21 | } else { 22 | cb(null, bundle) 23 | } 24 | } 25 | delete(id, cb) { 26 | const bundle = bundles[id] 27 | if(!bundle) { 28 | cb({code: 404}) 29 | } else { 30 | delete bundles[id] 31 | cb(null) 32 | } 33 | } 34 | addBook(id, pgid, cb) { 35 | const bundle = bundles[id] 36 | const book = books[pgid] 37 | if(!bundle || !book) { 38 | cb({code: 404}) 39 | } else { 40 | bundle.books.push({'id': pgid, 'title': book.title}) 41 | cb(null) 42 | } 43 | } 44 | } 45 | 46 | const bundles = {} 47 | const books = { 48 | 'pg132': { 49 | 'id': '132', 50 | 'title': 'The Art of War' 51 | }, 52 | 'pg2680': { 53 | 'id': '2680', 54 | 'title': 'Meditations', 55 | }, 56 | 'pg26203': { 57 | 'id': '26203', 58 | 'title': 'The Adventures of Tom Sawyer' 59 | } 60 | } 61 | 62 | 63 | function reportError(statusOk, err, res, body, cb) { 64 | if(err) { 65 | cb(err) 66 | return true 67 | } 68 | if(res.statusCode != statusOk) { 69 | cb({ 70 | code: res.statusCode, 71 | message: res.statusMessage, 72 | error: body 73 | }) 74 | return true 75 | } 76 | } 77 | 78 | module.exports = Bundle -------------------------------------------------------------------------------- /aula38-b4-webpack/app/bookSearch.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | b4 6 | 7 | 8 |
9 |

b4app

10 | 18 |
19 |

20 |
21 |
22 | 23 |
24 | 25 |
26 |
27 |
28 | 29 |
30 | 31 |
32 |
33 |
34 |
35 | 36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 | 45 | -------------------------------------------------------------------------------- /aula37-b4-front-end-templates/public/b4index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | b4 5 | 6 | 7 | 8 |
9 |

b4app

10 | 27 |

Better Book Bundle Builder

28 |

29 | A catalog of 57,000 books from Project Gutenberg. 30 | Moreover, it allows to create groups of books, named bundles, 31 | that aggregate a set of books. 32 |

33 |

References

34 | 46 |
47 | 48 | -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/app/views/main.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | b4 5 | 6 | 7 |
8 |

b4app

9 | 26 |
27 |
28 | 29 |
30 |

31 | 48 |
49 | 50 | -------------------------------------------------------------------------------- /aula36-b4-front-end-javascript/public/b4index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | b4 5 | 6 | 7 | 8 |
9 |

b4app

10 | 27 |

Better Book Bundle Builder

28 |

29 | A catalog of 57,000 books from Project Gutenberg. 30 | Moreover, it allows to create groups of books, named bundles, 31 | that aggregate a set of books. 32 |

33 |

References

34 | 46 |
47 | 48 | -------------------------------------------------------------------------------- /aula33-b4-web-server-promises/lib/bundle-mock.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const rp = require('request-promise') 4 | 5 | class Bundle { 6 | 7 | static init(es) { 8 | return new Bundle(es) 9 | } 10 | 11 | /** 12 | * @param {{host: string, port: number, books_index: string, bundle_index: string}} es 13 | */ 14 | constructor(es){ 15 | this.bundlesUrl = `http://${es.host}:${es.port}/${es.bundles_index}/bundle` 16 | this.booksUrl = `http://${es.host}:${es.port}/${es.books_index}/book` 17 | } 18 | 19 | create(name) { 20 | const id = 127 21 | bundles[id] = { 22 | 'name': name, 23 | 'books': [] 24 | } 25 | return Promise.resolve({ '_id': id}) 26 | } 27 | get(id) { 28 | const bundle = bundles[id] 29 | return !bundle 30 | ? Promise.reject({statusCode: 404}) 31 | : Promise.resolve(bundle) 32 | } 33 | delete(id) { 34 | const bundle = bundles[id] 35 | if(!bundle) 36 | return Promise.reject({statusCode: 404}) //cb({code: 404}) 37 | delete bundles[id] 38 | return Promise.resolve() //cb(null) 39 | } 40 | addBook(id, pgid) { 41 | const bundle = bundles[id] 42 | const book = books[pgid] 43 | if(!bundle || !book) 44 | return Promise.reject({statusCode: 404}) // cb({code: 404}) 45 | bundle.books.push({'id': pgid, 'title': book.title}) 46 | return Promise.resolve() //cb(null) 47 | } 48 | } 49 | 50 | const bundles = {} 51 | const books = { 52 | 'pg132': { 53 | 'id': '132', 54 | 'title': 'The Art of War' 55 | }, 56 | 'pg2680': { 57 | 'id': '2680', 58 | 'title': 'Meditations', 59 | }, 60 | 'pg26203': { 61 | 'id': '26203', 62 | 'title': 'The Adventures of Tom Sawyer' 63 | } 64 | } 65 | 66 | module.exports = Bundle -------------------------------------------------------------------------------- /aula43-b4-passport/auth-web-api.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const passport = require('passport') 4 | const auth = require('./lib/auth.js') 5 | 6 | const es = { 7 | host: 'localhost', 8 | port: '9200', 9 | } 10 | const authService = auth.init(es) 11 | 12 | module.exports = (app) => { 13 | 14 | passport.serializeUser((user, done) => done(null, user._id)) 15 | passport.deserializeUser((userId, done) => authService 16 | .getUser(userId) 17 | .then(user => done(null, user)) 18 | .catch(err => done(err)) 19 | ) 20 | app.use(passport.initialize()) 21 | app.use(passport.session()) 22 | 23 | app.get('/api/auth/session', getSession) 24 | app.post('/api/auth/login', login) 25 | app.post('/api/auth/logout', logout) 26 | app.post('/api/auth/signup', signup) 27 | 28 | function getSession(req, resp, next) { 29 | const fullname = req.isAuthenticated() ? req.user.fullname : undefined 30 | resp.json({ 31 | 'auth': req.isAuthenticated(), 32 | 'fullname': fullname 33 | }) 34 | } 35 | function login(req, resp, next) { 36 | authService 37 | .authenticate(req.body.username, req.body.password) 38 | .then(user => { 39 | req.login(user, (err) => { 40 | if(err) next(err) 41 | else resp.json(user) 42 | }) 43 | }) 44 | .catch(err => next(err)) 45 | } 46 | function logout(req, resp, next) { 47 | next({'statusCode': 500, 'err': 'Not implemented!'}) 48 | } 49 | function signup(req, resp, next) { 50 | authService 51 | .createUser(req.body.fullname, req.body.username, req.body.password) 52 | .then(user => { 53 | req.login(user, (err) => { 54 | if(err) next(err) 55 | else resp.json(user) 56 | }) 57 | }) 58 | } 59 | } -------------------------------------------------------------------------------- /aula42-b4-authentication/bundles-web-api.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Bundle = require('./lib/bundle') 4 | // const Bundle = require('./lib/bundle-mock') 5 | 6 | const es = { 7 | host: 'localhost', 8 | port: '9200', 9 | books_index: 'books', 10 | bundles_index: 'bundles' 11 | } 12 | 13 | const bundle = Bundle.init(es) 14 | 15 | module.exports = (app) => { 16 | app.post('/api/bundle', postBundle) 17 | app.get('/api/bundle', getBundles) 18 | app.get('/api/bundle/:id', getBundle) 19 | app.get('/api/books/search', bookSearch) 20 | app.use(resourceNotFond) 21 | app.use(errorHandler) 22 | 23 | function bookSearch(req, resp, next) { 24 | const title = req.query.title 25 | const authors = req.query.authors 26 | bundle 27 | .searchBook(title, authors) 28 | .then(data => resp.json(data)) 29 | .catch(next) 30 | } 31 | function postBundle(req, resp, next) { 32 | req.user = {'_id': 'zemanel'} 33 | const name = req.body.name 34 | bundle 35 | .create(req.user._id, name) 36 | .then(data => resp.json(data)) 37 | .catch(next) 38 | } 39 | function getBundles(req, resp, next) { 40 | req.user = {'_id': 'zemanel'} 41 | bundle 42 | .getAll(req.user._id) 43 | .then(data => resp.json(data)) 44 | .catch(next) 45 | } 46 | function getBundle(req, resp, next) { 47 | const id = req.params.id 48 | bundle 49 | .get(id) 50 | .then(bundle => resp.json(bundle)) 51 | .catch(next) 52 | } 53 | function resourceNotFond(req, resp, next) { 54 | next({ 55 | 'statusCode': 404, 56 | 'error': 'Resource Not Found!' 57 | }) 58 | } 59 | function errorHandler(err, req, res, next) { 60 | res.statusCode = err.statusCode || 500 61 | res.json(err.error) 62 | } 63 | } 64 | 65 | -------------------------------------------------------------------------------- /aula13-unit-tests/test/stream-json-parser-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const expect = require('chai').expect 4 | const EventEmitter = require('events').EventEmitter 5 | const StreamJsonParser = require('../lib/stream-json-parser') 6 | 7 | describe('StreamJsonParser', () => { 8 | let stream 9 | let client 10 | 11 | beforeEach(() => { 12 | stream = new EventEmitter() 13 | client = new StreamJsonParser(stream) 14 | }) 15 | 16 | it('should emit a message event from a single data event', done => { 17 | client.on('message', message => { 18 | expect(message) 19 | .to.be.an('object') 20 | .and.have.a.property('foo', 'bar') 21 | done() 22 | }) 23 | stream.emit('data', '{"foo":"bar"}\n\r') 24 | }) 25 | it('should emit a message event from a JSON string splitted in 2 data events', done => { 26 | client.on('message', message => { 27 | expect(message) 28 | .to.be.an('object') 29 | .and.have.a.property('foo', 'bar') 30 | done() 31 | }) 32 | stream.emit('data', '{"foo":"b') 33 | process.nextTick(() => stream.emit('data', 'ar"}\n\r')) 34 | }) 35 | it('should emit 2 messages events from one data event with 2 JSON objects', done => { 36 | const msg1 = {foo: 'bar'} 37 | const msg2 = {foo: 'zas'} 38 | let first = true 39 | client.on('message', message => { 40 | if(first) { 41 | expect(message) 42 | .to.be.an('object') 43 | .and.have.a.property('foo', msg1.foo) 44 | first = false 45 | } else { 46 | expect(message) 47 | .to.be.an('object') 48 | .and.have.a.property('foo', msg2.foo) 49 | done() 50 | } 51 | }) 52 | stream.emit('data', '{"foo":"bar"}\n\r{"foo":"zas"}\n\r') 53 | }) 54 | }) -------------------------------------------------------------------------------- /aula43-b4-passport/app/js/bundles.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Handlebars = require('./../../node_modules/handlebars/dist/handlebars.js') 4 | const util = require('./util.js') 5 | const bundlesCreateView = Handlebars.compile(require('./../views/bundlesCreate.hbs')) 6 | const bundlesListView = Handlebars.compile(require('./../views/bundlesList.hbs')) 7 | 8 | module.exports = async (divMain) => { 9 | 10 | 11 | try{ 12 | divMain.innerHTML = '' 13 | const session = await util.fetchJSON('/api/auth/session') 14 | if(!session.auth) 15 | return util.showAlert('You cannot access Bundles! Only for authenticated users.') 16 | divMain.innerHTML = bundlesCreateView() 17 | const view = await getBundlesView() 18 | divMain.insertAdjacentHTML('beforeend', view) 19 | document 20 | .getElementById('btNewBundle') 21 | .addEventListener('click', newBundleHandler) 22 | } 23 | catch(err){ 24 | util.showAlert(JSON.stringify(err)) 25 | } 26 | 27 | function newBundleHandler(ev) { 28 | ev.preventDefault() 29 | const txtBundleName = document.getElementById('txtBundleName') 30 | const url = 'http://localhost:3000/api/bundle' 31 | const options = { 32 | method: 'POST', 33 | body: `name=${txtBundleName.value}`, 34 | headers: { 35 | 'Content-Type': 'application/x-www-form-urlencoded' 36 | } 37 | } 38 | fetch(url, options) 39 | .then(getBundlesView) 40 | .then(view => { 41 | divMain.querySelector('table').remove() 42 | divMain.insertAdjacentHTML('beforeend', view) 43 | }) 44 | .catch(err => util.showAlert(err, 'danger')) 45 | } 46 | function getBundlesView() { 47 | return fetch('http://localhost:3000/api/bundle') 48 | .then(resp => resp.json()) 49 | .then(arr => bundlesListView({'bundles': arr})) 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /aula35-b4-static-files/public/b4index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | b4 5 | 6 | 7 | 22 | 23 | 24 |
25 |

b4app

26 | 33 |

Better Book Bundle Builder

34 |

35 | A catalog of 57,000 books from Project Gutenberg. 36 | Moreover, it allows to create groups of books, named bundles, 37 | that aggregate a set of books. 38 |

39 |

References

40 | 52 |
53 | 54 | -------------------------------------------------------------------------------- /aula38-b4-webpack/lib/bundle-mock.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | class Bundle { 4 | 5 | static init(es) { 6 | return new Bundle(es) 7 | } 8 | 9 | /** 10 | * @param {{host: string, port: number, books_index: string, bundle_index: string}} es 11 | */ 12 | constructor(es){ 13 | this.bundlesUrl = `http://${es.host}:${es.port}/${es.bundles_index}/bundle` 14 | this.booksUrl = `http://${es.host}:${es.port}/${es.books_index}/book` 15 | } 16 | 17 | create(name, cb) { 18 | const id = 127 19 | bundles[id] = { 20 | 'name': name, 21 | 'books': [] 22 | } 23 | cb(null, {'_id': id }) 24 | } 25 | get(id, cb) { 26 | const bundle = bundles[id] 27 | if(!bundle) { 28 | cb({code: 404}) 29 | } else { 30 | cb(null, bundle) 31 | } 32 | } 33 | delete(id, cb) { 34 | const bundle = bundles[id] 35 | if(!bundle) { 36 | cb({code: 404}) 37 | } else { 38 | delete bundles[id] 39 | cb(null) 40 | } 41 | } 42 | addBook(id, pgid, cb) { 43 | const bundle = bundles[id] 44 | const book = books[pgid] 45 | if(!bundle || !book) { 46 | cb({code: 404}) 47 | } else { 48 | bundle.books.push({'id': pgid, 'title': book.title}) 49 | cb(null) 50 | } 51 | } 52 | } 53 | 54 | const bundles = {} 55 | const books = { 56 | 'pg132': { 57 | 'id': '132', 58 | 'title': 'The Art of War' 59 | }, 60 | 'pg2680': { 61 | 'id': '2680', 62 | 'title': 'Meditations', 63 | }, 64 | 'pg26203': { 65 | 'id': '26203', 66 | 'title': 'The Adventures of Tom Sawyer' 67 | } 68 | } 69 | 70 | 71 | function reportError(statusOk, err, res, body, cb) { 72 | if(err) { 73 | cb(err) 74 | return true 75 | } 76 | if(res.statusCode != statusOk) { 77 | cb({ 78 | code: res.statusCode, 79 | message: res.statusMessage, 80 | error: body 81 | }) 82 | return true 83 | } 84 | } 85 | 86 | module.exports = Bundle -------------------------------------------------------------------------------- /aula43-b4-passport/lib/bundle-mock.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | class Bundle { 4 | 5 | static init(es) { 6 | return new Bundle(es) 7 | } 8 | 9 | /** 10 | * @param {{host: string, port: number, books_index: string, bundle_index: string}} es 11 | */ 12 | constructor(es){ 13 | this.bundlesUrl = `http://${es.host}:${es.port}/${es.bundles_index}/bundle` 14 | this.booksUrl = `http://${es.host}:${es.port}/${es.books_index}/book` 15 | } 16 | 17 | create(name, cb) { 18 | const id = 127 19 | bundles[id] = { 20 | 'name': name, 21 | 'books': [] 22 | } 23 | cb(null, {'_id': id }) 24 | } 25 | get(id, cb) { 26 | const bundle = bundles[id] 27 | if(!bundle) { 28 | cb({code: 404}) 29 | } else { 30 | cb(null, bundle) 31 | } 32 | } 33 | delete(id, cb) { 34 | const bundle = bundles[id] 35 | if(!bundle) { 36 | cb({code: 404}) 37 | } else { 38 | delete bundles[id] 39 | cb(null) 40 | } 41 | } 42 | addBook(id, pgid, cb) { 43 | const bundle = bundles[id] 44 | const book = books[pgid] 45 | if(!bundle || !book) { 46 | cb({code: 404}) 47 | } else { 48 | bundle.books.push({'id': pgid, 'title': book.title}) 49 | cb(null) 50 | } 51 | } 52 | } 53 | 54 | const bundles = {} 55 | const books = { 56 | 'pg132': { 57 | 'id': '132', 58 | 'title': 'The Art of War' 59 | }, 60 | 'pg2680': { 61 | 'id': '2680', 62 | 'title': 'Meditations', 63 | }, 64 | 'pg26203': { 65 | 'id': '26203', 66 | 'title': 'The Adventures of Tom Sawyer' 67 | } 68 | } 69 | 70 | 71 | function reportError(statusOk, err, res, body, cb) { 72 | if(err) { 73 | cb(err) 74 | return true 75 | } 76 | if(res.statusCode != statusOk) { 77 | cb({ 78 | code: res.statusCode, 79 | message: res.statusMessage, 80 | error: body 81 | }) 82 | return true 83 | } 84 | } 85 | 86 | module.exports = Bundle -------------------------------------------------------------------------------- /aula35-b4-static-files/lib/bundle-mock.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | class Bundle { 4 | 5 | static init(es) { 6 | return new Bundle(es) 7 | } 8 | 9 | /** 10 | * @param {{host: string, port: number, books_index: string, bundle_index: string}} es 11 | */ 12 | constructor(es){ 13 | this.bundlesUrl = `http://${es.host}:${es.port}/${es.bundles_index}/bundle` 14 | this.booksUrl = `http://${es.host}:${es.port}/${es.books_index}/book` 15 | } 16 | 17 | create(name, cb) { 18 | const id = 127 19 | bundles[id] = { 20 | 'name': name, 21 | 'books': [] 22 | } 23 | cb(null, {'_id': id }) 24 | } 25 | get(id, cb) { 26 | const bundle = bundles[id] 27 | if(!bundle) { 28 | cb({code: 404}) 29 | } else { 30 | cb(null, bundle) 31 | } 32 | } 33 | delete(id, cb) { 34 | const bundle = bundles[id] 35 | if(!bundle) { 36 | cb({code: 404}) 37 | } else { 38 | delete bundles[id] 39 | cb(null) 40 | } 41 | } 42 | addBook(id, pgid, cb) { 43 | const bundle = bundles[id] 44 | const book = books[pgid] 45 | if(!bundle || !book) { 46 | cb({code: 404}) 47 | } else { 48 | bundle.books.push({'id': pgid, 'title': book.title}) 49 | cb(null) 50 | } 51 | } 52 | } 53 | 54 | const bundles = {} 55 | const books = { 56 | 'pg132': { 57 | 'id': '132', 58 | 'title': 'The Art of War' 59 | }, 60 | 'pg2680': { 61 | 'id': '2680', 62 | 'title': 'Meditations', 63 | }, 64 | 'pg26203': { 65 | 'id': '26203', 66 | 'title': 'The Adventures of Tom Sawyer' 67 | } 68 | } 69 | 70 | 71 | function reportError(statusOk, err, res, body, cb) { 72 | if(err) { 73 | cb(err) 74 | return true 75 | } 76 | if(res.statusCode != statusOk) { 77 | cb({ 78 | code: res.statusCode, 79 | message: res.statusMessage, 80 | error: body 81 | }) 82 | return true 83 | } 84 | } 85 | 86 | module.exports = Bundle -------------------------------------------------------------------------------- /aula42-b4-authentication/lib/bundle-mock.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | class Bundle { 4 | 5 | static init(es) { 6 | return new Bundle(es) 7 | } 8 | 9 | /** 10 | * @param {{host: string, port: number, books_index: string, bundle_index: string}} es 11 | */ 12 | constructor(es){ 13 | this.bundlesUrl = `http://${es.host}:${es.port}/${es.bundles_index}/bundle` 14 | this.booksUrl = `http://${es.host}:${es.port}/${es.books_index}/book` 15 | } 16 | 17 | create(name, cb) { 18 | const id = 127 19 | bundles[id] = { 20 | 'name': name, 21 | 'books': [] 22 | } 23 | cb(null, {'_id': id }) 24 | } 25 | get(id, cb) { 26 | const bundle = bundles[id] 27 | if(!bundle) { 28 | cb({code: 404}) 29 | } else { 30 | cb(null, bundle) 31 | } 32 | } 33 | delete(id, cb) { 34 | const bundle = bundles[id] 35 | if(!bundle) { 36 | cb({code: 404}) 37 | } else { 38 | delete bundles[id] 39 | cb(null) 40 | } 41 | } 42 | addBook(id, pgid, cb) { 43 | const bundle = bundles[id] 44 | const book = books[pgid] 45 | if(!bundle || !book) { 46 | cb({code: 404}) 47 | } else { 48 | bundle.books.push({'id': pgid, 'title': book.title}) 49 | cb(null) 50 | } 51 | } 52 | } 53 | 54 | const bundles = {} 55 | const books = { 56 | 'pg132': { 57 | 'id': '132', 58 | 'title': 'The Art of War' 59 | }, 60 | 'pg2680': { 61 | 'id': '2680', 62 | 'title': 'Meditations', 63 | }, 64 | 'pg26203': { 65 | 'id': '26203', 66 | 'title': 'The Adventures of Tom Sawyer' 67 | } 68 | } 69 | 70 | 71 | function reportError(statusOk, err, res, body, cb) { 72 | if(err) { 73 | cb(err) 74 | return true 75 | } 76 | if(res.statusCode != statusOk) { 77 | cb({ 78 | code: res.statusCode, 79 | message: res.statusMessage, 80 | error: body 81 | }) 82 | return true 83 | } 84 | } 85 | 86 | module.exports = Bundle -------------------------------------------------------------------------------- /aula28-b4-web-server-Router/lib/bundle-mock.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | class Bundle { 4 | 5 | static init(es) { 6 | return new Bundle(es) 7 | } 8 | 9 | /** 10 | * @param {{host: string, port: number, books_index: string, bundle_index: string}} es 11 | */ 12 | constructor(es){ 13 | this.bundlesUrl = `http://${es.host}:${es.port}/${es.bundles_index}/bundle` 14 | this.booksUrl = `http://${es.host}:${es.port}/${es.books_index}/book` 15 | } 16 | 17 | create(name, cb) { 18 | const id = 127 19 | bundles[id] = { 20 | 'name': name, 21 | 'books': [] 22 | } 23 | cb(null, {'_id': id }) 24 | } 25 | get(id, cb) { 26 | const bundle = bundles[id] 27 | if(!bundle) { 28 | cb({code: 404}) 29 | } else { 30 | cb(null, bundle) 31 | } 32 | } 33 | delete(id, cb) { 34 | const bundle = bundles[id] 35 | if(!bundle) { 36 | cb({code: 404}) 37 | } else { 38 | delete bundles[id] 39 | cb(null) 40 | } 41 | } 42 | addBook(id, pgid, cb) { 43 | const bundle = bundles[id] 44 | const book = books[pgid] 45 | if(!bundle || !book) { 46 | cb({code: 404}) 47 | } else { 48 | bundle.books.push({'id': pgid, 'title': book.title}) 49 | cb(null) 50 | } 51 | } 52 | } 53 | 54 | const bundles = {} 55 | const books = { 56 | 'pg132': { 57 | 'id': '132', 58 | 'title': 'The Art of War' 59 | }, 60 | 'pg2680': { 61 | 'id': '2680', 62 | 'title': 'Meditations', 63 | }, 64 | 'pg26203': { 65 | 'id': '26203', 66 | 'title': 'The Adventures of Tom Sawyer' 67 | } 68 | } 69 | 70 | 71 | function reportError(statusOk, err, res, body, cb) { 72 | if(err) { 73 | cb(err) 74 | return true 75 | } 76 | if(res.statusCode != statusOk) { 77 | cb({ 78 | code: res.statusCode, 79 | message: res.statusMessage, 80 | error: body 81 | }) 82 | return true 83 | } 84 | } 85 | 86 | module.exports = Bundle -------------------------------------------------------------------------------- /aula31-b4-web-server-express/lib/bundle-mock.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | class Bundle { 4 | 5 | static init(es) { 6 | return new Bundle(es) 7 | } 8 | 9 | /** 10 | * @param {{host: string, port: number, books_index: string, bundle_index: string}} es 11 | */ 12 | constructor(es){ 13 | this.bundlesUrl = `http://${es.host}:${es.port}/${es.bundles_index}/bundle` 14 | this.booksUrl = `http://${es.host}:${es.port}/${es.books_index}/book` 15 | } 16 | 17 | create(name, cb) { 18 | const id = 127 19 | bundles[id] = { 20 | 'name': name, 21 | 'books': [] 22 | } 23 | cb(null, {'_id': id }) 24 | } 25 | get(id, cb) { 26 | const bundle = bundles[id] 27 | if(!bundle) { 28 | cb({code: 404}) 29 | } else { 30 | cb(null, bundle) 31 | } 32 | } 33 | delete(id, cb) { 34 | const bundle = bundles[id] 35 | if(!bundle) { 36 | cb({code: 404}) 37 | } else { 38 | delete bundles[id] 39 | cb(null) 40 | } 41 | } 42 | addBook(id, pgid, cb) { 43 | const bundle = bundles[id] 44 | const book = books[pgid] 45 | if(!bundle || !book) { 46 | cb({code: 404}) 47 | } else { 48 | bundle.books.push({'id': pgid, 'title': book.title}) 49 | cb(null) 50 | } 51 | } 52 | } 53 | 54 | const bundles = {} 55 | const books = { 56 | 'pg132': { 57 | 'id': '132', 58 | 'title': 'The Art of War' 59 | }, 60 | 'pg2680': { 61 | 'id': '2680', 62 | 'title': 'Meditations', 63 | }, 64 | 'pg26203': { 65 | 'id': '26203', 66 | 'title': 'The Adventures of Tom Sawyer' 67 | } 68 | } 69 | 70 | 71 | function reportError(statusOk, err, res, body, cb) { 72 | if(err) { 73 | cb(err) 74 | return true 75 | } 76 | if(res.statusCode != statusOk) { 77 | cb({ 78 | code: res.statusCode, 79 | message: res.statusMessage, 80 | error: body 81 | }) 82 | return true 83 | } 84 | } 85 | 86 | module.exports = Bundle -------------------------------------------------------------------------------- /aula37-b4-front-end-templates/lib/bundle-mock.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | class Bundle { 4 | 5 | static init(es) { 6 | return new Bundle(es) 7 | } 8 | 9 | /** 10 | * @param {{host: string, port: number, books_index: string, bundle_index: string}} es 11 | */ 12 | constructor(es){ 13 | this.bundlesUrl = `http://${es.host}:${es.port}/${es.bundles_index}/bundle` 14 | this.booksUrl = `http://${es.host}:${es.port}/${es.books_index}/book` 15 | } 16 | 17 | create(name, cb) { 18 | const id = 127 19 | bundles[id] = { 20 | 'name': name, 21 | 'books': [] 22 | } 23 | cb(null, {'_id': id }) 24 | } 25 | get(id, cb) { 26 | const bundle = bundles[id] 27 | if(!bundle) { 28 | cb({code: 404}) 29 | } else { 30 | cb(null, bundle) 31 | } 32 | } 33 | delete(id, cb) { 34 | const bundle = bundles[id] 35 | if(!bundle) { 36 | cb({code: 404}) 37 | } else { 38 | delete bundles[id] 39 | cb(null) 40 | } 41 | } 42 | addBook(id, pgid, cb) { 43 | const bundle = bundles[id] 44 | const book = books[pgid] 45 | if(!bundle || !book) { 46 | cb({code: 404}) 47 | } else { 48 | bundle.books.push({'id': pgid, 'title': book.title}) 49 | cb(null) 50 | } 51 | } 52 | } 53 | 54 | const bundles = {} 55 | const books = { 56 | 'pg132': { 57 | 'id': '132', 58 | 'title': 'The Art of War' 59 | }, 60 | 'pg2680': { 61 | 'id': '2680', 62 | 'title': 'Meditations', 63 | }, 64 | 'pg26203': { 65 | 'id': '26203', 66 | 'title': 'The Adventures of Tom Sawyer' 67 | } 68 | } 69 | 70 | 71 | function reportError(statusOk, err, res, body, cb) { 72 | if(err) { 73 | cb(err) 74 | return true 75 | } 76 | if(res.statusCode != statusOk) { 77 | cb({ 78 | code: res.statusCode, 79 | message: res.statusMessage, 80 | error: body 81 | }) 82 | return true 83 | } 84 | } 85 | 86 | module.exports = Bundle -------------------------------------------------------------------------------- /aula36-b4-front-end-javascript/lib/bundle-mock.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | class Bundle { 4 | 5 | static init(es) { 6 | return new Bundle(es) 7 | } 8 | 9 | /** 10 | * @param {{host: string, port: number, books_index: string, bundle_index: string}} es 11 | */ 12 | constructor(es){ 13 | this.bundlesUrl = `http://${es.host}:${es.port}/${es.bundles_index}/bundle` 14 | this.booksUrl = `http://${es.host}:${es.port}/${es.books_index}/book` 15 | } 16 | 17 | create(name, cb) { 18 | const id = 127 19 | bundles[id] = { 20 | 'name': name, 21 | 'books': [] 22 | } 23 | cb(null, {'_id': id }) 24 | } 25 | get(id, cb) { 26 | const bundle = bundles[id] 27 | if(!bundle) { 28 | cb({code: 404}) 29 | } else { 30 | cb(null, bundle) 31 | } 32 | } 33 | delete(id, cb) { 34 | const bundle = bundles[id] 35 | if(!bundle) { 36 | cb({code: 404}) 37 | } else { 38 | delete bundles[id] 39 | cb(null) 40 | } 41 | } 42 | addBook(id, pgid, cb) { 43 | const bundle = bundles[id] 44 | const book = books[pgid] 45 | if(!bundle || !book) { 46 | cb({code: 404}) 47 | } else { 48 | bundle.books.push({'id': pgid, 'title': book.title}) 49 | cb(null) 50 | } 51 | } 52 | } 53 | 54 | const bundles = {} 55 | const books = { 56 | 'pg132': { 57 | 'id': '132', 58 | 'title': 'The Art of War' 59 | }, 60 | 'pg2680': { 61 | 'id': '2680', 62 | 'title': 'Meditations', 63 | }, 64 | 'pg26203': { 65 | 'id': '26203', 66 | 'title': 'The Adventures of Tom Sawyer' 67 | } 68 | } 69 | 70 | 71 | function reportError(statusOk, err, res, body, cb) { 72 | if(err) { 73 | cb(err) 74 | return true 75 | } 76 | if(res.statusCode != statusOk) { 77 | cb({ 78 | code: res.statusCode, 79 | message: res.statusMessage, 80 | error: body 81 | }) 82 | return true 83 | } 84 | } 85 | 86 | module.exports = Bundle -------------------------------------------------------------------------------- /aula39-b4-SPA-hash-based-routing/lib/bundle-mock.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | class Bundle { 4 | 5 | static init(es) { 6 | return new Bundle(es) 7 | } 8 | 9 | /** 10 | * @param {{host: string, port: number, books_index: string, bundle_index: string}} es 11 | */ 12 | constructor(es){ 13 | this.bundlesUrl = `http://${es.host}:${es.port}/${es.bundles_index}/bundle` 14 | this.booksUrl = `http://${es.host}:${es.port}/${es.books_index}/book` 15 | } 16 | 17 | create(name, cb) { 18 | const id = 127 19 | bundles[id] = { 20 | 'name': name, 21 | 'books': [] 22 | } 23 | cb(null, {'_id': id }) 24 | } 25 | get(id, cb) { 26 | const bundle = bundles[id] 27 | if(!bundle) { 28 | cb({code: 404}) 29 | } else { 30 | cb(null, bundle) 31 | } 32 | } 33 | delete(id, cb) { 34 | const bundle = bundles[id] 35 | if(!bundle) { 36 | cb({code: 404}) 37 | } else { 38 | delete bundles[id] 39 | cb(null) 40 | } 41 | } 42 | addBook(id, pgid, cb) { 43 | const bundle = bundles[id] 44 | const book = books[pgid] 45 | if(!bundle || !book) { 46 | cb({code: 404}) 47 | } else { 48 | bundle.books.push({'id': pgid, 'title': book.title}) 49 | cb(null) 50 | } 51 | } 52 | } 53 | 54 | const bundles = {} 55 | const books = { 56 | 'pg132': { 57 | 'id': '132', 58 | 'title': 'The Art of War' 59 | }, 60 | 'pg2680': { 61 | 'id': '2680', 62 | 'title': 'Meditations', 63 | }, 64 | 'pg26203': { 65 | 'id': '26203', 66 | 'title': 'The Adventures of Tom Sawyer' 67 | } 68 | } 69 | 70 | 71 | function reportError(statusOk, err, res, body, cb) { 72 | if(err) { 73 | cb(err) 74 | return true 75 | } 76 | if(res.statusCode != statusOk) { 77 | cb({ 78 | code: res.statusCode, 79 | message: res.statusMessage, 80 | error: body 81 | }) 82 | return true 83 | } 84 | } 85 | 86 | module.exports = Bundle -------------------------------------------------------------------------------- /aula40-b4-SPA-hash-based-routing/lib/bundle-mock.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | class Bundle { 4 | 5 | static init(es) { 6 | return new Bundle(es) 7 | } 8 | 9 | /** 10 | * @param {{host: string, port: number, books_index: string, bundle_index: string}} es 11 | */ 12 | constructor(es){ 13 | this.bundlesUrl = `http://${es.host}:${es.port}/${es.bundles_index}/bundle` 14 | this.booksUrl = `http://${es.host}:${es.port}/${es.books_index}/book` 15 | } 16 | 17 | create(name, cb) { 18 | const id = 127 19 | bundles[id] = { 20 | 'name': name, 21 | 'books': [] 22 | } 23 | cb(null, {'_id': id }) 24 | } 25 | get(id, cb) { 26 | const bundle = bundles[id] 27 | if(!bundle) { 28 | cb({code: 404}) 29 | } else { 30 | cb(null, bundle) 31 | } 32 | } 33 | delete(id, cb) { 34 | const bundle = bundles[id] 35 | if(!bundle) { 36 | cb({code: 404}) 37 | } else { 38 | delete bundles[id] 39 | cb(null) 40 | } 41 | } 42 | addBook(id, pgid, cb) { 43 | const bundle = bundles[id] 44 | const book = books[pgid] 45 | if(!bundle || !book) { 46 | cb({code: 404}) 47 | } else { 48 | bundle.books.push({'id': pgid, 'title': book.title}) 49 | cb(null) 50 | } 51 | } 52 | } 53 | 54 | const bundles = {} 55 | const books = { 56 | 'pg132': { 57 | 'id': '132', 58 | 'title': 'The Art of War' 59 | }, 60 | 'pg2680': { 61 | 'id': '2680', 62 | 'title': 'Meditations', 63 | }, 64 | 'pg26203': { 65 | 'id': '26203', 66 | 'title': 'The Adventures of Tom Sawyer' 67 | } 68 | } 69 | 70 | 71 | function reportError(statusOk, err, res, body, cb) { 72 | if(err) { 73 | cb(err) 74 | return true 75 | } 76 | if(res.statusCode != statusOk) { 77 | cb({ 78 | code: res.statusCode, 79 | message: res.statusMessage, 80 | error: body 81 | }) 82 | return true 83 | } 84 | } 85 | 86 | module.exports = Bundle -------------------------------------------------------------------------------- /aula37-b4-front-end-templates/public/bookSearch.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | b4 6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 |

b4app

14 | 22 |
23 |

24 |
25 |
26 | 27 |
28 | 29 |
30 |
31 |
32 | 33 |
34 | 35 |
36 |
37 |
38 |
39 | 40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 | 49 | -------------------------------------------------------------------------------- /aula22-b4-web-server/bundles-web-api.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const url = require('url') 4 | // const Bundle = require('./lib/bundle') 5 | const Bundle = require('./lib/bundle-mock') 6 | 7 | const es = { 8 | host: 'localhost', 9 | port: '9200', 10 | books_index: 'books', 11 | bundles_index: 'bundles' 12 | } 13 | 14 | const bundle = Bundle.init(es) 15 | 16 | module.exports = (app) => { 17 | app.use(postBundle) 18 | app.use(getBundle) 19 | app.use(resourceNotFond) 20 | return app 21 | 22 | function postBundle(req, resp) { 23 | const {pathname, query} = url.parse(req.url, true) // true to parse also the query-string 24 | const method = req.method 25 | console.log(`${Date()}: request to ${pathname}`) 26 | 27 | if(method == 'POST' && pathname == '/api/bundle'){ 28 | const name = query.name 29 | bundle.create(name, (err, data) => { 30 | if(err) { 31 | resp.statusCode = err.code 32 | resp.end() 33 | } else { 34 | resp.statusCode = 200 35 | resp.end(JSON.stringify(data)) 36 | } 37 | }) 38 | // create a bundle 39 | return true 40 | } 41 | return false 42 | } 43 | 44 | function getBundle(req, resp) { 45 | const {pathname} = url.parse(req.url, true) // true to parse also the query-string 46 | const method = req.method 47 | console.log(`${Date()}: request to ${pathname}`) 48 | 49 | if(method == 'GET' && pathname.indexOf('/api/bundle') >= 0){ 50 | const id = pathname.split('/').pop() 51 | bundle.get(id, (err, bundle) => { 52 | if(err) { 53 | resp.statusCode = err.code 54 | resp.end() 55 | } else { 56 | resp.statusCode = 200 57 | resp.end(JSON.stringify(bundle)) 58 | } 59 | }) 60 | return true 61 | } 62 | return false 63 | } 64 | 65 | function resourceNotFond(req, resp) { 66 | resp.statusCode = 404 67 | resp.end('Resource Not Found!') 68 | return true 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /aula28-b4-web-server-Router/bundles-web-api.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const url = require('url') 4 | const Bundle = require('./lib/bundle') 5 | // const Bundle = require('./lib/bundle-mock') 6 | 7 | const es = { 8 | host: 'localhost', 9 | port: '9200', 10 | books_index: 'books', 11 | bundles_index: 'bundles' 12 | } 13 | 14 | const bundle = Bundle.init(es) 15 | 16 | module.exports = (app) => { 17 | app.use(postBundle) 18 | app.use(getBundle) 19 | app.use(resourceNotFond) 20 | return app 21 | 22 | function postBundle(req, resp) { 23 | const {pathname, query} = url.parse(req.url, true) // true to parse also the query-string 24 | const method = req.method 25 | console.log(`${Date()}: request to ${pathname}`) 26 | 27 | if(method == 'POST' && pathname == '/api/bundle'){ 28 | const name = query.name 29 | bundle.create(name, (err, data) => { 30 | if(err) { 31 | resp.statusCode = err.code 32 | resp.end() 33 | } else { 34 | resp.statusCode = 200 35 | resp.end(JSON.stringify(data)) 36 | } 37 | }) 38 | // create a bundle 39 | return true 40 | } 41 | return false 42 | } 43 | 44 | function getBundle(req, resp) { 45 | const {pathname} = url.parse(req.url, true) // true to parse also the query-string 46 | const method = req.method 47 | console.log(`${Date()}: request to ${pathname}`) 48 | 49 | if(method == 'GET' && pathname.indexOf('/api/bundle') >= 0){ 50 | const id = pathname.split('/').pop() 51 | bundle.get(id, (err, bundle) => { 52 | if(err) { 53 | resp.statusCode = err.code 54 | resp.end() 55 | } else { 56 | resp.statusCode = 200 57 | resp.end(JSON.stringify(bundle)) 58 | } 59 | }) 60 | return true 61 | } 62 | return false 63 | } 64 | 65 | function resourceNotFond(req, resp) { 66 | resp.statusCode = 404 67 | resp.end('Resource Not Found!') 68 | return true 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /aula20-b4-Better-Book-Bundle-Builder/lib/bundle.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const request = require('request') 4 | 5 | class Bundle { 6 | 7 | /** 8 | * @param {{host: string, port: number, books_index: string, bundle_index: string}} es 9 | */ 10 | constructor(es){ 11 | this.bundlesUrl = `http://${es.host}:${es.port}/${es.bundles_index}/bundle` 12 | this.booksUrl = `http://${es.host}:${es.port}/${es.books_index}/book` 13 | } 14 | /** 15 | * @param {{host: string, port: number, books_index: string, bundle_index: string}} es 16 | */ 17 | static init(es) { 18 | return new Bundle(es) 19 | } 20 | 21 | create(name, cb) { 22 | const options = { 23 | 'uri': this.bundlesUrl, 24 | 'json': true, 25 | 'body': { 'name': name, 'books': []} 26 | } 27 | request.post(options, (err, res, body) =>{ 28 | if(err) 29 | return cb(err) 30 | if(res.statusCode != 201) { 31 | return cb({ 32 | code: res.statusCode, 33 | message: res.statusMessage, 34 | error: body 35 | }) 36 | } 37 | cb(null, body) 38 | }) 39 | } 40 | get(id, cb) { 41 | const uri = `${this.bundlesUrl}/${id}` 42 | request.get(uri, (err, res, body) =>{ 43 | if(err) 44 | return cb(err) 45 | if(res.statusCode != 200) { 46 | return cb({ 47 | code: res.statusCode, 48 | message: res.statusMessage, 49 | error: body 50 | }) 51 | } 52 | cb(null, JSON.parse(body)._source) 53 | }) 54 | } 55 | delete(id, cb) { 56 | const uri = `${this.bundlesUrl}/${id}` 57 | request.delete(uri, (err, res, body) =>{ 58 | if(err) 59 | return cb(err) 60 | if(res.statusCode != 200) { 61 | return cb({ 62 | code: res.statusCode, 63 | message: res.statusMessage, 64 | error: body 65 | }) 66 | } 67 | cb(null, JSON.parse(body)) 68 | }) 69 | } 70 | } 71 | 72 | module.exports = Bundle --------------------------------------------------------------------------------