Node examples, and minor fixes
This commit is contained in:
+28
-11
@@ -5,11 +5,9 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "flow-remove-types src/ -d out/",
|
"build": "flow-remove-types src/ -d out/",
|
||||||
"buildWithMaps":
|
"buildWithMaps": "flow-remove-types src/ -d out/ --pretty --sourcemaps",
|
||||||
"flow-remove-types src/ -d out/ --pretty --sourcemaps",
|
|
||||||
"start": "npm run build && node out/doroundmath.js",
|
"start": "npm run build && node out/doroundmath.js",
|
||||||
"nodemon":
|
"nodemon": "nodemon --watch src --delay 1 --exec npm start",
|
||||||
"./node_modules/nodemon/bin/nodemon.js --watch src --delay 1 --exec npm start",
|
|
||||||
"addTypes": "flow-typed install",
|
"addTypes": "flow-typed install",
|
||||||
"update": "npm install && flow-typed install",
|
"update": "npm install && flow-typed install",
|
||||||
"flow": "flow",
|
"flow": "flow",
|
||||||
@@ -20,7 +18,10 @@
|
|||||||
"author": "Federico Kereki",
|
"author": "Federico Kereki",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"babel": {
|
"babel": {
|
||||||
"presets": ["env", "flow"]
|
"presets": [
|
||||||
|
"env",
|
||||||
|
"flow"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"eslintConfig": {
|
"eslintConfig": {
|
||||||
"parserOptions": {
|
"parserOptions": {
|
||||||
@@ -33,21 +34,37 @@
|
|||||||
"node": true,
|
"node": true,
|
||||||
"es6": true
|
"es6": true
|
||||||
},
|
},
|
||||||
"extends": ["eslint:recommended", "plugin:flowtype/recommended"],
|
"extends": [
|
||||||
"plugins": ["babel", "flowtype"],
|
"eslint:recommended",
|
||||||
|
"plugin:flowtype/recommended"
|
||||||
|
],
|
||||||
|
"plugins": [
|
||||||
|
"babel",
|
||||||
|
"flowtype"
|
||||||
|
],
|
||||||
"rules": {
|
"rules": {
|
||||||
"no-console": "off",
|
"no-console": "off",
|
||||||
"no-var": "error",
|
"no-var": "error",
|
||||||
"prefer-const": "error"
|
"prefer-const": "error"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"eslintIgnore": ["**/out/*.js"],
|
"eslintIgnore": [
|
||||||
|
"**/out/*.js"
|
||||||
|
],
|
||||||
"flow-coverage-report": {
|
"flow-coverage-report": {
|
||||||
"concurrentFiles": 1,
|
"concurrentFiles": 1,
|
||||||
"excludeGlob": ["node_modules/**"],
|
"excludeGlob": [
|
||||||
"includeGlob": ["src/**/*.js"],
|
"node_modules/**"
|
||||||
|
],
|
||||||
|
"includeGlob": [
|
||||||
|
"src/**/*.js"
|
||||||
|
],
|
||||||
"threshold": 90,
|
"threshold": 90,
|
||||||
"type": ["text", "html", "json"]
|
"type": [
|
||||||
|
"text",
|
||||||
|
"html",
|
||||||
|
"json"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"prettier": {
|
"prettier": {
|
||||||
"tabWidth": 4,
|
"tabWidth": 4,
|
||||||
|
|||||||
@@ -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(multR(22.9, 12.4)); // 283.96
|
||||||
console.log(divR(22, 7)); // 3.14
|
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
|
||||||
|
|||||||
@@ -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/");
|
||||||
@@ -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);
|
||||||
@@ -1,13 +1,17 @@
|
|||||||
/* @flow */
|
/* @flow */
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
// These won't be exported:
|
||||||
|
|
||||||
const roundToCents = (x: number): number => Math.round(x * 100) / 100;
|
const roundToCents = (x: number): number => Math.round(x * 100) / 100;
|
||||||
|
|
||||||
const changeSign = (x: number): number => -x;
|
const changeSign = (x: number): number => -x;
|
||||||
|
|
||||||
|
// The following will be exported:
|
||||||
|
|
||||||
const addR = (x: number, y: number): number => roundToCents(x + y);
|
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);
|
const multR = (x: number, y: number): number => roundToCents(x * y);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user