From 2a1c0794883fb141b82e5f90e9413079f9a60530 Mon Sep 17 00:00:00 2001 From: Ethan Brown Date: Sun, 12 Jan 2020 17:14:06 -0800 Subject: [PATCH] Chapter 4 cleanup. --- README.md | 4 ++++ ch04/README.md | 23 +++++++++++++++++++++++ ch04/lib/fortune.js | 1 + 3 files changed, 28 insertions(+) create mode 100644 ch04/README.md diff --git a/README.md b/README.md index 254855d..40208bd 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,7 @@ The [Chapter 2 Examples](ch02/README.md) demonstrate a simple, minimal web serve ### Chapter 3 The [Chapter 3 Examples](ch03/README.md) demonstrate a simple, minimal web server using Express. + +### Chapter 4 + +The [Chapter 4 Example](ch03/README.md) take the "fortune cookie" functionality deveoped in Chapter 3, and implement it as a Node module. diff --git a/ch04/README.md b/ch04/README.md new file mode 100644 index 0000000..9c06fbd --- /dev/null +++ b/ch04/README.md @@ -0,0 +1,23 @@ +# Chapter 4 Examples - Web Development with Node and Express, 2nd Edition + +## Using Node Modules + +This chapter is mostly background information, so there's only one example in this chapter; taking the "fortunte cookie" functionality that was developed in Chapter 3, and placing it in a Node module. See _lib/fortune.js_ and _meadowlark.js_. + +### Setup + +The example in this chapter has Node module dependencies (`express` and `express-handlebars`). These dependencies are listed in the _package.json_ file. However, when you first clone this repo, you won't have them installed (_package.json_ is simply a manifest). To install them, simply run: + +``` +npm install +``` + +### Running + +Minimal example; uses Express, but doesn't do very much. To run: + +``` +node 00-meadowlark +``` + +Then visit _http://localhost:3000/about_ in your browser. diff --git a/ch04/lib/fortune.js b/ch04/lib/fortune.js index 6e29c0a..2f34006 100644 --- a/ch04/lib/fortune.js +++ b/ch04/lib/fortune.js @@ -5,6 +5,7 @@ const fortuneCookies = [ "You will have a pleasant surprise.", "Whenever possible, keep it simple.", ] + exports.getFortune = () => { const idx = Math.floor(Math.random()*fortuneCookies.length) return fortuneCookies[idx]