From 1fd98ed5c5c30408384a365005261b27a5db9d07 Mon Sep 17 00:00:00 2001 From: fkereki Date: Sat, 7 Apr 2018 13:54:46 -0300 Subject: [PATCH] Node examples, and minor fixes --- chapter03/package.json | 39 ++++++++++++++++++++-------- chapter03/src/doroundmath.js | 2 +- chapter03/src/miniserver.js | 13 ++++++++++ chapter03/src/promisify.js | 49 ++++++++++++++++++++++++++++++++++++ chapter03/src/roundmath.js | 6 ++++- 5 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 chapter03/src/miniserver.js create mode 100644 chapter03/src/promisify.js diff --git a/chapter03/package.json b/chapter03/package.json index d54d267..3c4b064 100644 --- a/chapter03/package.json +++ b/chapter03/package.json @@ -5,11 +5,9 @@ "main": "index.js", "scripts": { "build": "flow-remove-types src/ -d out/", - "buildWithMaps": - "flow-remove-types src/ -d out/ --pretty --sourcemaps", + "buildWithMaps": "flow-remove-types src/ -d out/ --pretty --sourcemaps", "start": "npm run build && node out/doroundmath.js", - "nodemon": - "./node_modules/nodemon/bin/nodemon.js --watch src --delay 1 --exec npm start", + "nodemon": "nodemon --watch src --delay 1 --exec npm start", "addTypes": "flow-typed install", "update": "npm install && flow-typed install", "flow": "flow", @@ -20,7 +18,10 @@ "author": "Federico Kereki", "license": "ISC", "babel": { - "presets": ["env", "flow"] + "presets": [ + "env", + "flow" + ] }, "eslintConfig": { "parserOptions": { @@ -33,21 +34,37 @@ "node": true, "es6": true }, - "extends": ["eslint:recommended", "plugin:flowtype/recommended"], - "plugins": ["babel", "flowtype"], + "extends": [ + "eslint:recommended", + "plugin:flowtype/recommended" + ], + "plugins": [ + "babel", + "flowtype" + ], "rules": { "no-console": "off", "no-var": "error", "prefer-const": "error" } }, - "eslintIgnore": ["**/out/*.js"], + "eslintIgnore": [ + "**/out/*.js" + ], "flow-coverage-report": { "concurrentFiles": 1, - "excludeGlob": ["node_modules/**"], - "includeGlob": ["src/**/*.js"], + "excludeGlob": [ + "node_modules/**" + ], + "includeGlob": [ + "src/**/*.js" + ], "threshold": 90, - "type": ["text", "html", "json"] + "type": [ + "text", + "html", + "json" + ] }, "prettier": { "tabWidth": 4, diff --git a/chapter03/src/doroundmath.js b/chapter03/src/doroundmath.js index 3fe9259..b031080 100644 --- a/chapter03/src/doroundmath.js +++ b/chapter03/src/doroundmath.js @@ -8,4 +8,4 @@ console.log(RM.addR(12.348, 4.221)); // 16.57 console.log(multR(22.9, 12.4)); // 283.96 console.log(divR(22, 7)); // 3.14 -// console.log(RM.changeSign(0.07)); // error; RM.changeSign is not a function +// console.log(RM.changeSign(0.07)); // error; RM.changeSign is not a function diff --git a/chapter03/src/miniserver.js b/chapter03/src/miniserver.js new file mode 100644 index 0000000..febed80 --- /dev/null +++ b/chapter03/src/miniserver.js @@ -0,0 +1,13 @@ +/* @flow */ +"use strict"; + +const http = require("http"); + +http + .createServer((req, res) => { + res.writeHead(200, { "Content-Type": "text/plain" }); + res.end("Server alive!"); + }) + .listen(8080, "localhost"); + +console.log("Mini server ready at http://localhost:8080/"); diff --git a/chapter03/src/promisify.js b/chapter03/src/promisify.js new file mode 100644 index 0000000..969c3fd --- /dev/null +++ b/chapter03/src/promisify.js @@ -0,0 +1,49 @@ +/* @flow */ +"use strict"; + +const fs = require("fs"); +const util = require("util"); + +const FILE_TO_READ = "/home/fkereki/MODERNJS/chapter03/src/promisify.js"; // its own source! + +// 1. Original error-first style callback + +function showFileLength1(fileName: string): void { + fs.readFile(fileName, "utf8", (err, text) => { + if (err) { + throw err; + } else { + console.log(`1. Reading, old style: ${text.length} bytes`); + } + }); +} +showFileLength1(FILE_TO_READ); + +// 2. Alternative style using promises + +function showFileLength2(fileName: string): void { + const readFile = util.promisify(fs.readFile); + + readFile(fileName, "utf8") + .then((text: string) => { + console.log(`2. Reading with promises: ${text.length} bytes`); + }) + .catch((err: mixed) => { + throw err; + }); +} +showFileLength2(FILE_TO_READ); + +// 3. Using async/await, and with an arrow function just for variety + +const showFileLength3 = async (fileName: string) => { + const readFile = util.promisify(fs.readFile); + + try { + const text: string = await readFile(fileName, "utf8"); + console.log(`3. Reading with async/await: ${text.length} bytes`); + } catch (err) { + throw err; + } +}; +showFileLength3(FILE_TO_READ); diff --git a/chapter03/src/roundmath.js b/chapter03/src/roundmath.js index 47a2079..8e71b57 100644 --- a/chapter03/src/roundmath.js +++ b/chapter03/src/roundmath.js @@ -1,13 +1,17 @@ /* @flow */ "use strict"; +// These won't be exported: + const roundToCents = (x: number): number => Math.round(x * 100) / 100; const changeSign = (x: number): number => -x; +// The following will be exported: + const addR = (x: number, y: number): number => roundToCents(x + y); -const subR = (x: number, y: number) => addR(x, changeSign(y)); +const subR = (x: number, y: number): number => addR(x, changeSign(y)); const multR = (x: number, y: number): number => roundToCents(x * y);