ga-wdi-boston / node-api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

General Assembly Logo

Introduction to the Node API

Prerequisites

  • Familiarity with JavaScript as a language.
  • Familiarity with JavaScript in the browser environment.

Objectives

By the end of this, developers should be able to:

  • Reference the Node API documentation for using JavaScript outside the browser.
  • Reference the MDN JavaScript documentation for JavaScript language features.
  • Write a Node script using the File System API.

Preparation

  1. Fork and clone this repository.
  2. Install dependencies with npm install.

JavaScript, the Browser, and Node

  • JavaScript: a language specified by ECMA and implemented independently
  • Browser: an environment for running JavaScript (among other things)
  • Node: an environment for running JavaScript outside the browser

Somewhat valid equations:

Browser = JavaScript + Browser API + (other things)
   Node = JavaScript + CLI/Server API

How are the two environments similar? How do they differ?

Lab: Research the File System API

Take a few minutes to read the following API documentation for the Node File System module.

While you're reading, imagine how you'd use each function. Write some example code in your notebook. Try to explain what each function does in your own words, including what sorts of arguments it takes and each argument's type.

If you finish early, look for other interesting functions in the File System module.

  1. fs.readdir(path, callback)
  2. fs.readdirSync(path)
  3. fs.readFile(file[, options], callback)
  4. fs.writeFile(file, data[, options], callback)

Discussion

Are these methods JavaScript, node, or browser? Math.random fs.readFile http.ClientRequest document.ready new Promise

Annotate-Along: lib/copy-json.js

Code-Along: hey-yall.js

"Hello, World!" with the Node file system API.

Lab: Write a Randomizer

Randomize the lines in a file.

How do you shuffle an array?

Run your code using: node bin/randomizer.js example.txt

Bonus

Write a node script that takes a JSON string and parses into an object.

Input: "{ names: ["first last", "jon smith", "chris payne"]" }

Output:

{ names:  {
  first: "last",
  jon: "smith",
  chris: "payne" }
}

Additional Resources

  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.

About

License:Other


Languages

Language:JavaScript 100.0%